Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

SESSION doesn't work with POST request

140 views
Skip to first unread message

Marcin

unread,
Apr 28, 2020, 11:21:24 AM4/28/20
to Fat-Free Framework
Hello,
I love your framework! However recently I bumped on a problem. I can't set and get session when I use POST request. It only works with GET request. Do you know any fix for that?

First test:

echo $_SESSION["favanimal"];
$_SESSION
["favanimal"] = "cat";


Second test:

$cache=Cache::instance();
$sessionCache
=new Cache('folder=core/sessions/');
$session
=new Session(NULL,NULL,$sessionCache);
var_dump
($f3->get('SESSION'));
$f3
->set('SESSION.sth', "works");

Thank you!

Marcin

ved

unread,
Apr 28, 2020, 12:12:57 PM4/28/20
to Fat-Free Framework
Hi,

Your examples are not helpful as they're basically just a couple of random tests without any context.
What exactly is not working? Where/what are the get or post requests you mention?
Please post the code and routes as you are using them on your app otherwise nobody will be able to figure it out.

Cheers

ikkez

unread,
Apr 28, 2020, 12:15:11 PM4/28/20
to f3-fra...@googlegroups.com
Hi Marcin,

what exactly does not work? To set a variable in a Cache-bases Session? In your example code, it's not clear what is called on GET or POST and also is the cache + session initialisation also happening on get request?
Is the POST request made from ajax or another device? Is the session cookie sent on POST? Would be easier if we might have more information about your case.

v.

unread,
Apr 29, 2020, 3:28:41 AM4/29/20
to Fat-Free Framework
Initialize sessions in your index.php file and point a route to the below function.

public function test()
{
 
if($f3->verb==="POST")
 
{
     $f3
->set('SESSION.var', "session vars just work");
 
}
  echo
"SESSION VAR : ".$f3->get('SESSION.var');
  echo
"<form method='post' action=''><input value='press the button' type='submit'></form>"

}


Code untested, but the idea works

Marcin

unread,
Apr 29, 2020, 3:55:00 AM4/29/20
to Fat-Free Framework
I run it a little modified and it works when I open it in browser. It doesn't work with Jquery ajax call.

var website="https://domain.com";  
$
.ajax({
  type
: 'POST',
  url
: website+'/api/test1',
  cache
: false,
  data
: {}
})



    $f3->route('GET|POST /api/test1',
     
function ($f3){
       
if($f3->get('POST'))
       
{
            $f3
->set('SESSION.var', "session vars just work1");
       
}
        echo
"POST? : "; var_dump($f3->get('POST')) ;
        echo
"SESSION VAR : ".$f3->get('SESSION.var');
        echo
"<form method='post' action=''><input name='cosss' value='cos' type='text'> <input value='press the button' type='submit'></form>";  
   
});


 To set a variable in a Cache-bases Session? 
Yes. 
 
In your example code, it's not clear what is called on GET or POST and also is the cache + session initialisation also happening on get request?
 As showed before.

Is the POST request made from ajax or another device? 
Ajax from another website.
Is the session cookie sent on POST? 
I don't know. How can I check?

Marcin

unread,
Apr 29, 2020, 4:12:55 AM4/29/20
to Fat-Free Framework
Ok.
It works with this:

PHP
$f3->set('CORS.origin',$domain);  
$f3
->set('CORS.credentials',true);

JS:
$.ajax({ type: 'POST', url: url, cache: false, xhrFields: { withCredentials: true });


Somebody explained it here: https://stackoverflow.com/posts/23660618/timeline
Reply all
Reply to author
Forward
0 new messages