Hi, I have a code that works for session time out. But when I tried to incorporate other stuff it doesn't work. Please help to fix the issue.
My working code has these files index.html, first.php, ajax.js, timecheck.php, and check.php
Here is the code for each
index.html
<html><body>This is the default page. Clicking the link below will login the user.<a href="first.php">Click here to login</a><br/></body></html>
first.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'<br/><br/><br/><p><a href="">Back to article</a></p>';
echo'</body>';
echo'</html>';
}
ajax.js
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;
}
}
if (!xmlReq && typeof XMLHttpRequest != 'undefined') {
xmlReq = new XMLHttpRequest();
}
xmlReq.open('get', 'check.php', true);
xmlReq.setRequestHeader("Connection", "close");
xmlReq.send(null);
xmlReq.onreadystatechange = function(){
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";
}
}
}
}
timecheck.php
<?php
function checkIfTimedOut()
{
$current = time();// take the current time
$diff = $current - $_SESSION['loggedAt'];
if($diff > $_SESSION['timeOut'])
{
return true;
}
else
{
return false;
}
}
?>
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;
}
}
}
?>
It works fine upto this point. Now, I replaced index.html with index.php and first.php with loggedin.php and correspondingly changed the names wherever they appear in the rest of the files.
Here are my replacement file codes.
index.php
<html>
<?php
session_start();
require_once 'config.php';
require_once 'login3.php';
if (isset($_SESSION['user'])){
$user=$_SESSION['user'];
$res = mysql_query("SELECT fname,lname,city, state,aboutme,username FROM userprofile WHERE username = '$user'") or die (mysql_error());
$data = mysql_fetch_assoc($res);
$fname = htmlspecialchars($data['fname']);
$resa = mysql_query("SELECT id,fname,lname, email,picture FROM userprofile WHERE rank = '1'") or die (mysql_error());
$dataa = mysql_fetch_assoc($resa);
$fname1 = htmlspecialchars($dataa['fname']);
echo "<td><div align=right> Welcome " .$fname;"</div></td>"
?><body>
<?php include("indexheader.php"); ?>
<div style="display:inline-block; width: 1580px; overflow:hidden;">
<table align="center"><tr><td><table><tr><td> <div style="display:inline-block; width: 231px; overflow:hidden;"><B>Featured Workers</B></div>
</td></tr></table>
<div style="display:inline-block; width: 2155px; overflow:hidden;">
<table style='table-layout:fixed'>
<td> <table style='table-layout:fixed'><tr> <td>
<div style="display:inline-block; width: 50px; overflow:hidden;">
<img src="<?php echo $picture1; ?>" border="0" width="50" height="50" /></div></td>
<td><div style="display:inline-block; width: 150px; overflow:hidden;"><?php echo $fname1 . " ". $lname1 . "<br>" . $profession1; ?></div></td>
<TABLE style="table-layout:fixed;">
<tr><td><div style="display:inline-block; width: 165px; overflow:hidden;">
<center><u><a href="mailto:<?php echo $email1;?>?Subject=Interested%20in your%20services%20from%20Ripple"><?php echo "Contact" . " " . $fname1 . " ". $lname1; ?></a></u></center></div></td>
<td><div style="display:inline-block; width: 10px; overflow:hidden;"></div>
</td><td><div style="display:inline-block; width: 50px; overflow:hidden;"></div>
</td><table style='table-layout:fixed'><tr><td>
<div style="display:inline-block; width: 50px; overflow:hidden;"><img src="<?php echo $pictureb1; ?>" border="0" width="50" height="50" /></div></td>
<td><div style="display:inline-block; width: 230px; overflow:hidden;">
<?php echo $trading_or_business_as_name1 . "<br>" . $category_of_business1; ?></div></td></div></div></td></tr></table>
<?php
}else{
?>
<?php
$resa = mysql_query("SELECT id,fname,lname, email,picture FROM userprofile WHERE rank = '1'") or die (mysql_error());
$dataa = mysql_fetch_assoc($resa);
$fname1 = htmlspecialchars($dataa['fname']);
?>
< html><body>
<?php include("indexpageheader.php"); ?>
<?php
}
?>
<!-- BEGIN FOOTER -->
<?php include("footer.php"); ?>
</body>
</html>
loggedin.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
session_start();
require_once 'config.php';
if (isset($_SESSION['user'])){
$user=$_SESSION['user'];
$res = mysql_query("SELECT fname,lname,city, state,aboutme,username,picture FROM userprofile WHERE username = '$user'") or die (mysql_error());
$data = mysql_fetch_assoc($res);
$fname = htmlspecialchars($data['fname']);
$lname = htmlspecialchars($data['lname']);
$city = htmlspecialchars($data['city']);
$state = htmlspecialchars($data['state']);
$aboutme = htmlspecialchars($data['aboutme']);
$pic = htmlspecialchars($data['picture']);
echo '<td><div><p style="text-align: right;">Welcome ' .$fname;'</p></div></td>';
?>
<?php
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.php");
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'</body>';
echo'</html>';
}
?>
<body>
<?php include("loggedinheader.php"); ?><body>
<table style='table-layout:fixed'><tr><td><div style="display:inline-block; width: 175px; overflow:hidden;"><img src="/india/images/accountmanagement.png" border="0" /></div>
<div style="display:inline-block; width: 175px; overflow:hidden;"><img src="/india/images/profilemanagement.png" border="0"/></div>
<div style="display:inline-block; width: 175px; overflow:hidden;"><img src="/india/images/adsposted.png"border="0" /></div>
<div style="display:inline-block; width: 175px; overflow:hidden;"><img src="/india/images/adstracked.png" border="0" /></div>
<div style="display:inline-block; width: 175px; overflow:hidden;"><img src="/india/images/reviewsposted.png" border="0" /></div>
<div style="display:inline-block; width: 175px; overflow:hidden;"><img src="/india/images/customersupport.png" border="0"/></div>
</div></td></td></td>
<td><div style="display:inline-block; width: 460px; overflow:hidden;"><?php echo $fname . " ". $lname ." ". $city . ", " . $state . "<br />" . "<br />" . $aboutme; ?></div></td>
<table border="0" style="border-width: 1px; width:540px; height:75px; border-color:#000000;
border-style: solid;">
<tr>
<td>
<div style="display:inline-block; width: 165px; overflow:hidden;">
</div>
</td>
</tr>
</table>
</div>
<div style="display:inline-block; width: 720px; overflow:hidden;">
<table border="0" style="border-width: 1px; width:540px; height:75px; border-color:#000000;
border-style: solid;">
<tr>
<td>
<div style="display:inline-block; width: 165px; overflow:hidden;">
</div>
</td>
</tr>
</table>
</div>
<?php
}else{
?>
<?php
}
?>
<!-- BEGIN FOOTER -->
<?php include("footer.php"); ?>
This is the error I get :
Fatal error: Call to undefined function showLoggedIn() in D:\Hosting\xxxxxxx\html\india\loggedin.php on line 33
I'm not able to understand why because it works fine standalone.