I have an Apache2 web server with PHP running on my Raspberry Pi and I'm using sessions for storing the user's ID when they log in. I noticed that when the browser closes, the session is destroyed. How do I make the session last longer (ideally forever until they log out)?
            Asked
            
        
        
            Active
            
        
            Viewed 760 times
        
    0
            
            
        - 
                    1Possible duplicate of [How to change the session timeout in PHP?](https://stackoverflow.com/questions/8311320/how-to-change-the-session-timeout-in-php) – zanderwar Jun 02 '19 at 02:21
- 
                    Also, your browser could be destroying the PHPSESSID cookie on close, incognito does that for example – zanderwar Jun 02 '19 at 02:22
- 
                    For that you need to edit system files and it is quite a boring task. Why don't you just store data as cookie and retrive them anytime you want? It's simple and easy. – Best Bibek Jun 02 '19 at 02:25
- 
                    Can users manually put cookies into their browser? That's what I was worried about. – liaquore Jun 02 '19 at 03:39
- 
                    My answer did solve your issue? – Alessandro Jun 08 '19 at 08:54
1 Answers
2
            You can use a special setting of session_start() provided by PHP 7.0... with the following setting the session expires after 1 day (86400 seconds)... you can adjust this value as you want... even if you close the browser the session persists, to terminate your session you'll have to invoke session_destroy()...
if (session_status() == PHP_SESSION_NONE) {
  if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
    session_start(['cookie_lifetime' => 86400,]);
  } else {
    session_start();
  }
}
I hope this helps.
 
    
    
        Alessandro
        
- 900
- 12
- 23
