Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve auto logoff in php?

Tags:

php

I need auto-logoff system in my application.

if user not using the application more than thirty minutes.they should log in again.

this is what i need. lastAccessTime should be lesser than 30 minutes. if lastAccessTime exceeds than 30 minutes user should login again with their credentials.

(currently i auto log off using Lastaccesstime field in My user table (database) and compare lastaccesstime with current time for every page loads, I do not think this is right way.)

is their any way to achieve? Thanks in advance.

like image 789
Ramakrishnan Avatar asked May 25 '10 17:05

Ramakrishnan


2 Answers

You should specify the SESSION LIFETIME and just use the $_SESSION to see if a user is logged in:

ini_set('session.cookie_lifetime',(60*30)); // 60 seconds times 30 = 30 minutes
like image 183
Konerak Avatar answered Oct 23 '22 17:10

Konerak


If you're using cookies to keep users logged in, simply set an adequate TTL for it.

For a 30 minute expiration time, on login set the cookie this way:

setcookie($COOKIE_NAME, $COOKIE_VALUE, time() + 60 * 30);

Alternatively, you could use session_set_cookie_params

session_set_cookie_params(60 * 30); // takes lifetime as first argument
like image 45
kamasheto Avatar answered Oct 23 '22 18:10

kamasheto