Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto logout if browser is closed [duplicate]

Tags:

php

I have a requirement like, if user closes browser then upon the next access of the site user should be asked to login again: I have written the following code in .htaccess file which is working fine:

<IfModule mod_php5.c>    
    #Session timeout    
    php_value session.cookie_lifetime 1200    
    php_value session.gc_maxlifetime 1200    
</IfModule>    

I am excited to know am I doing the correct thing, or is there any other best way to do this?

like image 405
Srikanta Avatar asked Dec 06 '25 04:12

Srikanta


1 Answers

Set the session cookie parameters before you start your session.

session_set_cookie_params(0);
session_start();

Zero means "until the browser is closed".

This solution would be my personal "best" way of doing it, because you can control it per PHP-script, instead of site-wide. Also, it is clear from the PHP code, instead of relying on server-dependent settings.

like image 160
Bart Friederichs Avatar answered Dec 08 '25 18:12

Bart Friederichs