Symfony2 sessions not working as expected / session keeps timing out

前端 未结 5 1873
后悔当初
后悔当初 2021-01-03 00:31

My Symfony2 application displays a main page, and from there on it primarily uses AJAX requests to display content to the user via modals.

I\'ve noticed that after t

相关标签:
5条回答
  • 2021-01-03 00:42

    I set remember me cookie set to default, and then in security.yml

    security:
        firewalls:
            main:
                form_login:
                    remember_me: true
                remember_me:
                    key: mycookie
                    lifetime: 2592000 # 30 days
                    path: /
                    domain: ~
                    always_remember_me: true
    
    0 讨论(0)
  • 2021-01-03 00:48

    My first answer seems not suitable for your issue. Maybe this one will help.

    Do you clear Symfony cache between your requests ?

    Extract of symfony documentation :

    save_path

    type: string default: %kernel.cache.dir%/sessions

    This determines the argument to be passed to the save handler. If you choose the default file handler, this is the path where the session files are created. For more information, see Configuring the Directory where Session Files are Saved.

    You can also set this value to the save_path of your php.ini by setting the value to null.

    By default, Symfony stores sessions in the cache directory that is emptied while clearing cache...

    0 讨论(0)
  • 2021-01-03 00:51

    Travis T, I went the simplest route of all. I said

    nano /etc/cron.d/php5

    This opened the file showing the tremendously long crontab code that purges your session by default every 30 mins. The script was preceded by a #, and all I did was uncomment both lines by removing the #. So:

    #  Look for and purge old sessions every 30 minutes
    #  09, 39, *  * * *  root  @[ -x /usr/lib/php5/maxlifetime ] && [  etc 
       it's a long file.....]
    

    I just removed the 2 #'s in front of Look and 09. That's it !

    0 讨论(0)
  • 2021-01-03 00:58

    Extract of symfony documentation :

    cookie_lifetime

    type: integer default: null

    This determines the lifetime of the session - in seconds. It will use null by default, which means session.cookie_lifetime value from php.ini will be used. Setting this value to 0 means the cookie is valid for the length of the browser session.

    So, 0 is not infinite session BUT browser session... You should define a big amoutn of seconds and test it.

    0 讨论(0)
  • 2021-01-03 01:04

    The problem:

    It turns out that on Debian / Ubuntu systems, there is a system cronjob which runs every 30 minutes, cleaning out all "old" sessions. Herein lies the problem.

    The cronjob doesn't actually know what constitutes "old". The cronjob simply calls a PHP-CLI script located at /usr/lib/php5/maxlifetime which then removes all session files that exceed a certain age. Since the script is involved by PHP-CLI, and independently of Symfony2, it has no idea what values you specified for gc_maxlifetime and cookie_lifetime in your Symfony2 config file. Instead, if just defaults to using the session.cookie_lifetime and session.gc_maxlifetime values from the /etc/php5/cli/php.ini config file, which by default, is 24 minutes. So no matter what you specify in your Symfony2 config file, if you are idle for too long, your session will be removed.


    The solution:

    • Either delete the cronjob file at /etc/cron.d/php5 or,
    • Store your sessions in a database where they can't be touched by the cronjob
    0 讨论(0)
提交回复
热议问题