SESSION variables not working from controller to controller??

1,048 views
Skip to first unread message

Augusto Canessa

unread,
Jun 17, 2015, 11:58:34 AM6/17/15
to f3-fra...@googlegroups.com
Hi!
I don't know what I am doing wrong here. I'm working on a login / logout module.
I auth against the DB to know is the user credentials are ok, if they are.....i want to set a session variable and use it in the controllers beforeRoute to check. 
Problem is that, the variables comes empty from one controller to the other....

In the same controller, this prints "123" 

$this->f3->set('SESSION.z',123);
echo "VAR:". $this->f3->get('SESSION.z');

this prints nothing:

controller 1
$this->f3->set('SESSION.z',123);

controller 2
echo "VAR:". $this->f3->get('SESSION.z');

Here is my code:

class BaseController{
    protected $f3;    
    function __construct() { 
        $f3=Base::instance();
        $this->f3=$f3;
    }
       
}



class LoginController extends BaseController{
public function index(){
$view=new View;
        echo $view->render('app/views/login/LoginView.php');
}         
        public function check(){         
            $username = $this->f3->get('POST.username'); 
            $password = $this->f3->get('POST.password');
            $db = new \DB\Mongo('mongodb://XXXXXXXXXXX','XXXXXXXXX');
            $user = new \DB\Mongo\Mapper($db, 'user');
            $auth = new \Auth($user, array('id'=>'username', 'pw'=>'password'));
            $result = $auth->login($username,$password); // returns true on successful login
            if ($result == 1){
                $feedback = "";
                //new \DB\Mongo\Session($db);
                $this->f3->set('SESSION.z',123);
                echo "VAR:". $this->f3->get('SESSION.z'); //PRINTS 123
                
                //$this->f3->reroute('/home'); //-------------GO TO HOME!
            }else{
                $feedback = "Username or password incorrect";
            }
            
            $this->f3->set('username',$username);
            $this->f3->set('password',$password);
            $this->f3->set('feedback',$feedback);
            $view=new View;
            echo $view->render('app/views/login/LoginView.php');
        }

}

class HomeController extends BaseController{
public function index(){        
            echo "VAR:". $this->f3->get('SESSION.z'); //<----------------------EMPTY VALUE....
$view=new View;
        echo $view->render('app/views/home/HomeView.php');
}


}


xfra35

unread,
Jun 18, 2015, 3:19:11 AM6/18/15
to f3-fra...@googlegroups.com
Strange.. maybe you can try this very simple snippet, just to check that no application code interferes:

$f3->route('GET /test',function($f3){
  if ($var=$f3->get('GET.var')) {
    $f3->set('SESSION.var',$var);
    $f3->reroute($f3->PATH);
  }
  echo $f3->get('SESSION.var');
});

A call to "/test?var=foo" should output "foo".

If it doesn't work, have a look at PHP session configuration, in particular the settings related to cookies.

Augusto Canessa

unread,
Jun 18, 2015, 10:20:37 AM6/18/15
to xfra35 via Fat-Free Framework
Hi! Thank you for the help, but it's not working :(

I thought about php.ini but I have a few Joomlas and two Moodles + WordPress and no problem with sessions there....

What am I doing wrong here?

On Thu, Jun 18, 2015 at 4:19 AM, xfra35 via Fat-Free Framework <f3-framework+APn2wQdllyYRCbrJQTu...@googlegroups.com> wrote:
Strange.. maybe you can try this very simple snippet, just to check that no application code interferes:

$f3->route('GET /test',function($f3){
  if ($var=$f3->get('GET.var')) {
    $f3->set('SESSION.var',$var);
    $f3->reroute($f3->PATH);
  }
  echo $f3->get('SESSION.var');
});

A call to "/test?var=foo" should output "foo".

If it doesn't work, have a look at PHP settings related to sessions: http://php.net/manual/en/session.configuration.php, in particular those related to cookies.

--
You received this message because you are subscribed to a topic in the Google Groups "Fat-Free Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/f3-framework/LpSR3ZGi0K0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to f3-framework...@googlegroups.com.
To post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at http://groups.google.com/group/f3-framework.
For more options, visit https://groups.google.com/d/optout.

Dim Kas

unread,
Jun 22, 2015, 7:04:46 AM6/22/15
to f3-fra...@googlegroups.com
You can you use Chrome's dev tools to check if cookies are properly saved. It should be under the Resources tab

Anatol Buchholz

unread,
Jun 23, 2015, 1:33:00 AM6/23/15
to f3-fra...@googlegroups.com
Is it working for you with a normal php session syntax?

$f3->route('GET /test',function($f3){
  if ($var=$f3->get('GET.var')) {
     session_start();
     $_SESSION['var'] = $var;
     $f3->reroute('/stest');
  }
});

$f3->route('GET /stest',
    function() {
        session_start();
        echo $_SESSION['var'] ."<br>";
        echo session_save_path();
    }
);

Further as Dim Kas suggests check if your browser has a session cookie with developer toolbar +  check if you have corresponding file in your session_save_path() if not I would guess something is wrong with your setup php.ini etc or wrong chmod of your session directory.

Augusto Canessa

unread,
Jun 23, 2015, 10:11:40 AM6/23/15
to Anatol Buchholz via Fat-Free Framework
Hi, I tried this code by xfra3 and It didn't work. So I went to use sessions with mongo in the end, and that worked. I have to come back and see what the problem was, is in my to do list.
Thanks!!!

$f3->route('GET /test',function($f3){
  if ($var=$f3->get('GET.var')) {
    $f3->set('SESSION.var',$var);
    $f3->reroute($f3->PATH);
  }
  echo $f3->get('SESSION.var');
});

A call to "/test?var=foo" should output "foo".



On Tue, Jun 23, 2015 at 2:33 AM, Anatol Buchholz via Fat-Free Framework <f3-framework+APn2wQePWq2rp2JE73X...@googlegroups.com> wrote:
Is it working for you with a normal php session?

$f3->route('GET /test',function($f3){
  if ($var=$f3->get('GET.var')) 
session_start();{
    $f3->set('SESSION.var',$var);
    $f3->reroute($f3->PATH);
  }
  echo $f3->get('SESSION.var');
});

--

Dim Kas

unread,
Jun 24, 2015, 4:08:14 AM6/24/15
to f3-fra...@googlegroups.com
What session handler have you enabled? Cache? SQL?

ethanpil

unread,
Jun 24, 2015, 11:48:19 AM6/24/15
to f3-fra...@googlegroups.com
Don't know if this helps you, but I was having similar issues because I was using  die() to output some json_encode() and it was preventing the session vars from setting.

Naturo Diffusion

unread,
Apr 3, 2018, 2:20:08 PM4/3/18
to Fat-Free Framework
[SOLVED]   F3 SESSION variables fine : https://fatfreeframework.com/session 


This is old post is 2015 dated :  

Ysguy

unread,
Apr 4, 2018, 12:34:22 AM4/4/18
to f3-fra...@googlegroups.com
I figured out my issue. I was calling $db->begin(); and not using it throughout the rest of my controller file. This (somehow) caused all the $f3->set('SESSION.VAR','VAL'); to not stick. 
Reply all
Reply to author
Forward
0 new messages