douhuitan5863 2015-11-23 23:17
浏览 51
已采纳

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?

  • 写回答

2条回答 默认 最新

  • dongxi1680 2015-11-23 23:30
    关注

    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条)

报告相同问题?