我有一个网页,我有一个 session_check 文件,用于检查会话变量。我require 'session_check.php'
在需要的网页顶部添加了。我也曾经echo
检查过session_check.php
文件是否被调用。
我在 session_check.php 中有一个函数,它在注销时被调用,但它永远不会被调用。你能帮帮我吗?谢谢 :)
代码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);
}
?>
调用网页的部分代码
<?php
require 'session_check.php';
mysql_close($db_link);
?>
<!DOCTYPE html>
<html>.....</html>