我有一个网页,我有一个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>
答案 0 :(得分:1)
我写了下面的代码正在工作。请输入这些代码,你会得到解决方案。如果你想使用require()方法从第一页调用第二页,只需在第一页中调用,require(secondpage url)并删除第二页中的session_start()行。
第一页(samplephp.php)
<?php
session_start();
$_SESSION['host']="localhost";
$_SESSION['dbusername']="root";
$_SESSION['dbname']="userinfo";
$_SESSION['username']="testuser";
$_SESSION['authkey']="1";
?>
<!DOCTYPE html>
<html><head></head><body>test<form action="session_check.php"><input type="submit" value="click here"></form></body></html>
第二页(session_check.php)
<?php
session_start();
//Database Connection
$db_host = $_SESSION['host'];
$db_uname =$_SESSION['dbusername'];
$db_pwd = "";
$db_name = $_SESSION['dbname'];
$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,$db_link){
$query = "UPDATE usertable SET flag=1 WHERE username='$uname'";
mysql_query($query, $db_link);
mysql_close($db_link);
session_destroy();
echo "success";
//header('Location:index.php');
exit();
}
//Getting session variables
session_regenerate_id();
$cur_authkey = $_SESSION['authkey'];
$uname =$_SESSION['username'];
//Session data checking
$query = "SELECT flag FROM usertable 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['flag']){
user_logout($uname,$db_link);
}
?>
答案 1 :(得分:0)
在第一个session_start()
代码
<?php