0

我有一个网页,我有一个 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>
4

2 回答 2

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);
        }    
    ?>
于 2013-02-05T07:11:59.703 回答
0

在第一个标签session_start()之后的代码开头使用<?php

于 2013-02-03T14:29:24.540 回答