ANIL KUMAR PANIGRAHI 's Blog

Inactive in site then auto logout using php

Posted by: Anil Kumar Panigrahi in: ● November 10, 2009

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 :

<?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= getSqlQuery($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= getSqlQuery($updateQuery) or mysql_error();
$_SESSION['loginTime']=$current_time;
}

$_SESSION['user_id']=$resGetAdmin['user_id'];
header(“Location: secure.html”);
exit;
}
}
else
{
header(‘Location:’.$currentUrl.”);
exit;
}
}
?>

In the Remaining pages which are in the secure pages of site append the below code:

<?php include(“timeout.php”); //added; contains timeout info ?>

The timeout.php code is below:

<? 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:

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

Share on Twitter

1 Comment to "Inactive in site then auto logout using php"

Write Comment

About Me

  Anil Kumar Panigrahi
  Software Engineer (PHP)
  Blogger & Founder of Anil Labs
  Baruva, India

You can share with me . . .

My Own Contributions

1) First contribution for codeigniter framework ( multi language support using google translate API ). we can get files from http://github.com/nyros/codeigniter_multilanguage


2) Audio and Video Streaming using FFMpeg and PHP -complete doc file : http://www.docstoc.com/docs/8300349/Audio-and-Video-Streaming

Twitter Updates

Follow me @anil2u
  • http://www.google.co.uk/ logo is changing with mouse move . . . - posted on 07/09/2010 10:57:33
  • True enemies are real friends . . . - posted on 06/09/2010 10:15:29
  • Just watched nice movie " Madrasapattinam ". - posted on 05/09/2010 16:20:48
  • We will always be thankful to our teacher for all the hard work and efforts they have put in, for educating us.- Happy teachers Day . . . :) - posted on 05/09/2010 05:39:13
  • Working on shipping cost functionality (UPS and USPS) for oscommerce shopping cart - posted on 01/09/2010 09:40:03