I have a webpage and I have a session_check file that is used to check the session variables. I added require 'session_check.php' at the top of the webpages where it is required. I have also used echo to check that the session_check.php file got called.
I have a function in session_check.php that gets called when it's time to log out, but it never gets called. Could you please help me out? Thanks :)
Code for session_check.php:
<?php
//Database Connection
$db_host = $_SERVER['DB_HOST'];
$db_uname = $_SERVER['DB_UNAME'];
$db_pwd = $_SERVER['DB_PWD'];
$db_name = $_SERVER['DB_DB'];
$db_link = mysql_connect($db_host,$db_uname,$db_pwd);
if(!$db_link){
die("Could Not Connect:".mysql_error($db_link));
}
mysql_select_db($db_name, $db_link) or die('Can\'t use db:'. mysql_error($db_link));
//Logout function
function user_logout($uname){
$query = "UPDATE user SET last_used_token='' WHERE username='$uname'";
mysql_close($db_link);
session_destroy();
header('Location:index.php');
exit();
}
//Getting session variables
session_start();
session_regenerate_id();
$cur_authkey = $_SESSION['authkey'];
$uname = $_SESSION['username'];
//Session data checking
$query = "SELECT last_used_token FROM user WHERE username='$uname'";
$result = mysql_query($query, $db_link) or die('Error while updating auth key <br /> Query:'.$query.'MySQL error no:'.mysql_errno().'<br /> MySQL error:'.mysql_error($db_link));
$row = mysql_fetch_assoc($result);
if($cur_authkey != $row['last_used_token']){
user_logout($uname);
}
?>
Partial Code of the calling webpage
<?php
require 'session_check.php';
mysql_close($db_link);
?>
<!DOCTYPE html>
<html>.....</html>
mysql_escape_string($_SESSION['authkey']);?