Hi friends, I have the requirement that when a user login in the site and he/she inactive in the site more than 5 minutes then user will logout from the site.
For this i followed below code and i succeed,
In the login.php file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php session_start(); include("dbconfig.php"); if(isset($_POST)) { $current_time =time(); $current_date=date("d-m-Y"); extract($_POST); $query ="select * from users where user_email='".$textfield."' and user_pword='".$textfield2."' and user_status='1' "; $rs_user= mysql_query($query); $no_rows = mysql_num_rows($rs_user); if($no_rows > 0){ while($resGetAdmin = mysql_fetch_assoc($rs_user)){ $selQuery="select * from login where user_id=".$resGetAdmin['user_id'] ." AND loginDate ='$current_date'"; $rgetData= mysql_query($selQuery); $num_login = getSqlNumber($selQuery); if($num_login==0) { $insQuery= "insert into login (user_id,loginTime,loginDate,count) values (".$resGetAdmin['user_id'].",'$current_time','$current_date',1)"; $login_user= mysql_query($insQuery); $insert_id=mysql_insert_id(); $_SESSION['loginTime']=$current_time; }else{ $count = mysql_result($rgetData,0,’count’); $count = $count+1; $updateQuery= "update login set loginTime='$current_time' ,loginDate='$current_date' , count ='$count' where user_id=".$resGetAdmin['user_id'] ." AND loginDate='$current_date'"; $login_user= mysql_query($updateQuery) or mysql_error(); $_SESSION['loginTime']=$current_time; } $_SESSION['user_id']=$resGetAdmin['user_id']; header("Location: secure.html"); exit; } } else { header('Location:'.$currentUrl); exit; } } ?> |
Include the file
In the Remaining pages which are in the secure pages of site append the below code:
1 | <?php include("timeout.php"); //added; contains timeout info ?> |
The timeout.php code is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php session_start(); $timeout_length = 300; $current_time =time(); $current_date=date('d-m-Y'); if ($current_time – $_SESSION['loginTime'] > $timeout_length) { session_unregister(user_id); header("Location:redirect.html"); exit; } else { $_SESSION['loginTime'] = $current_time; $updateQuery= "update login set loginTime='".$_SESSION['loginTime']."', loginDate='$current_date' where user_id=".$_SESSION['user_id']." AND loginDate='$current_date'"; $login_user= mysql_query($updateQuery) or mysql_error(); } ?> |
The Database table for login table:
1 2 3 4 5 6 7 8 | CREATE TABLE IF NOT EXISTS `login` ( `login_id` int(11) NOT NULL auto_increment, `user_id` int(11) NOT NULL, `loginTime` varchar(255) NOT NULL, `loginDate` varchar(255) NOT NULL, `count` int(11) NOT NULL, PRIMARY KEY (`login_id`) ) |
It will be useful . . .
Thank you,
Anil Kumar Panigrahi



















very useful
Good script, But is it possible to do the same without using Database
Thanks
im a newbie in PHP.
is it possible to do the same without using Database
which i can only add – – on each secured page
thanks,