Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic Logout after 15 minutes of inactive in php

Tags:

php

I want to destroy session if users are not doing any kind of activity on website. At that time after 5 users automatically redirect on index page. How is it possible? Is possible in php with session handling and for that I have to maintain or update user login time or not..

like image 417
Prashant Bhatt Avatar asked Dec 11 '13 10:12

Prashant Bhatt


People also ask

How do I log off my computer after inactivity?

Step 1: Right click on the desktop, and select Personalize option. Step 2: From the left side panel click on Lock Screen and select Screen saver settings. Step 3: From the drop down bar under screen saver select an option. Step 4: Check the box On resume, display logon screen, change the number to 5 in the Wait box.

How can destroy session after some time in PHP?

It can be done by clicking on the logout button or by destroying that session after a fixed time. By default the expiry time of any particular session that is created is 1440 secs i.e. (24*60) i.e. 24 minutes. But in some cases, we need to change the default time accordingly.

How do I stay logged in to PHP?

Basically, we have to store both the Username and the Password in the user's browser as cookies. Then every time the page loads the session variable will be set. Hence the user can log in without having to enter the Username and Password again until the life of that cookie expires.


1 Answers

This is relatively easy to achive with this small snippet here:

 if(time() - $_SESSION['timestamp'] > 900) { //subtract new timestamp from the old one     echo"<script>alert('15 Minutes over!');</script>";     unset($_SESSION['username'], $_SESSION['password'], $_SESSION['timestamp']);     $_SESSION['logged_in'] = false;     header("Location: " . index.php); //redirect to index.php     exit; } else {     $_SESSION['timestamp'] = time(); //set new timestamp } 
like image 51
Realitätsverlust Avatar answered Sep 20 '22 01:09

Realitätsverlust