hi friends,
i am new to php.
i have source code to logout from a page when that page is inactive for certain seconds.
and it is logging out .
but the thing is it is logging out even when my mouse and keyboard is active. i.e. even when i am moving or clicking my mouse pointer on that page.
please tell me what else i need to include in this code.
here is the code:
window.onload = init;
var interval;
function init()
{
interval = setInterval(trackLogin,1000);
}
function trackLogin()
{
var xmlReq = false;
try {
xmlReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlReq = false;
}
}
//to communicate with a server after a web page has loaded.
if (!xmlReq && typeof XMLHttpRequest != 'undefined') {
//for instantiating an XMLHttpRequest object
xmlReq = new XMLHttpRequest();
}
xmlReq.open('get', 'check.php', true);
xmlReq.setRequestHeader("Connection", "close");
xmlReq.send(null);
xmlReq.onreadystatechange = function(){
// The 4 state means for the request response is ready and sent by the server..
//200 if page is found and 404 if the page is not found.
if(xmlReq.readyState == 4 && xmlReq.status==200) {
if(xmlReq.responseText == 1)
{
clearInterval(interval);
alert('You have been logged out.You will now be redirected to home page.');
document.location.href = "index.html";
}
}
}
}
check.php
<?php
session_start();
$stillLoggedIn = timeCheck();
echo $stillLoggedIn;
function timeCheck()
{
if(!isset($_SESSION['isLoggedIn']) || !($_SESSION['isLoggedIn']))
{
session_unset();
return true;
exit;
}
else
{
// user is logged in
require 'timeCheck.php';
$hasSessionExpired = checkIfTimedOut();
if($hasSessionExpired)
{
session_unset();
return true;
}
else
{
return false;
}
timecheck.php
<?php
function checkIfTimedOut()
{
$current = time();// take the current time
$diff = $current - $_SESSION['loggedAt'];
if($diff > $_SESSION['timeOut'])
{
return true;
}
else
{
return false;
}
}
?>
firstpage.php
<?php
session_start();
if(!isset($_SESSION['isLoggedIn']) || !($_SESSION['isLoggedIn']))
{
//code for authentication comes here
//ASSUME USER IS VALID
$_SESSION['isLoggedIn'] = true;
/////////////////////////////////////////
$_SESSION['timeOut'] = 5;
$logged = time();
$_SESSION['loggedAt']= $logged;
showLoggedIn();
}
else
{
require 'timeCheck.php';
$hasSessionExpired = checkIfTimedOut();
if($hasSessionExpired)
{
session_unset();
header("Location:index.html");
exit;
}
else
{
$_SESSION['loggedAt']= time();// update last accessed time
showLoggedIn();
}
}
function showLoggedIn()
{
echo'<html>';
echo'<head>';
echo'<script type="text/javascript" src="ajax.js"></script>';
echo'</head>';
echo'<body>';
echo'<p>';
echo'Page 1. User is logged in currently.Timeout has been set to 5 seconds. If you stay inactive for more then 5 seconds, you will be logged out automatically and redirected to home page.';
echo'</p>';
echo'<br/>';
echo'<p><a href="second.php">Go to second page</a></p>';
echo'<br/><br/><br/><p><a href="">Back to article</a></p>';
echo'</body>';
echo'</html>';
}
similarly second page.