Laravel 4.2简单会话逻辑不起作用

This is very simple session logic. I don't know why this isn't working. Here the code.

if(Session::get('sign_up_data')){
    echo 1;
}else{
    echo 2;
    Session::put('sign_up_data',21);
}
exit;

Always echoing 2 every time i reload the page. I am expecting echoing 2 on first load then the next 1 and 1 and 1 and soon. Tried different approach but still getting the same result. Do you any idea guys?

If you kill the application mid-cycle (through exit, dd, etc.), the session data won't write. Remove the exit, and you are good.

If you need to kill the script mid-cycle, then save the session data manually. So, in other words, this works:

if(Session::get('sign_up_data')){
    echo 1;
}else{
    echo 2;
    Session::put('sign_up_data',21);
}
// exit;

If you are going to kill the script, then call save manually like this:

if(Session::get('sign_up_data')){
    echo 1;
}else{
    echo 2;
    Session::put('sign_up_data',21);
    Session::save();
}
exit;
  1. Check that your config for the session driver isn't set to array. Arrays aren't persistent.

  2. Use chrome/firefox dev tools to check that a session cookie is being created and is the same every time you reload

As a final point you can simplify you code to use the default response feature (which will not solve your problem but is better code)/