为什么不会/如何排序PHP会话变量而不是取消设置

时间:2014-01-23 06:32:33

标签: php session session-variables session-cookies

似乎在我的所有尝试中,会话变量在被指示时都没有被取消。我尝试过的方法是:

session_unset();
session_destroy();
unset($_SESSION['name']);
$_SESSION = array();

全部合并。我已经用var_dump验证了变量本身仍然保存了值。我尝试使用标头来禁用浏览器缓存:

$ts = gmdate("D, d M Y H:i:s") . " GMT";
header("Expires: $ts");
header("Last-Modified: $ts");
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");  

(以及以上所有的组合)但据我所知,这甚至不是问题。 取消设置变量的唯一方法就是关闭浏览器。我理解会话UID存储在一个特殊的? cookie,但不知道如何直接操作/删除它,甚至不确定这是否是解决方案(虽然我唯一的想法)。

完整代码..

INDEX.HTM:

<html>
<?php
$ts = gmdate("D, d M Y H:i:s") . " GMT";
header("Expires: $ts");
header("Last-Modified: $ts");
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
?>

<a href = "new_user.htm"> New User</a><br>
<a href = "Login.htm"> User Login</a><br>
<a href = "search.htm"> Search</a><br>
<a href = "member.php"> Member</a><br>

</html>

Logout.htm(会话变量无法使用等):

<html>

<?php
$ts = gmdate("D, d M Y H:i:s") . " GMT";
header("Expires: $ts");
header("Last-Modified: $ts");
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");

session_start();

unset($_SESSION['name']);
session_unset();
session_destroy();
$_SESSION = array();

?>
Logout Succesful.<br><br>
<a href = "index.htm">index</a>

</html>

member.php(转储会话var的地方):

<?php
$ts = gmdate("D, d M Y H:i:s") . " GMT";
header("Expires: $ts");
header("Last-Modified: $ts");
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");

session_start();

if(!isset($_SESSION['user']))
{

$_SESSION = array();
session_destroy();
header("location:index.htm");
//echo $_SESSION['user'];
}

?>

<html>
<body>

<?php 
$uu = $_SESSION['user'];
var_dump($uu);
?>

Main User Page.++Code++
(seperate pages atm for ease+speed of writing,<br>
embedded stuff would take a bit longer)<br><br>
<a href = "addservice.php"> Add Service </a><br>
<a href = "additem.php"> Add Item </a><br>
<a href = "search.htm"> Search </a><br><br>
<a href = "logout.htm"> Logout </a><br><br>

</body>
</html>

的login.php:

<html>

<?php

$username="root";
$password="";
$database="test_db";
$con=mysqli_connect("localhost",$username,"",$database);
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

?>

<form action="log_in.php" method="post">
UserName: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>

<input type="submit"><br>
</form>

<?php

?>

</html>

导致log_in.php(设置会话变量的地方):

<?php
$ts = gmdate("D, d M Y H:i:s") . " GMT";
header("Expires: $ts");
header("Last-Modified: $ts");
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");

$username="root";
$password="";
$database="test_db";
$con=mysqli_connect("localhost",$username,"",$database);

if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

$username_l = $_POST['username'];
$pword_l = $_POST['password'];

$q_usercheck = "SELECT * FROM user WHERE User_Name='$username_l'";
$q_pwcheck = "SELECT * FROM user WHERE User_Name = '$username_l' and Password = '$pword_l'";

$uc_result = mysqli_query($con, $q_usercheck);
$uc_count = mysqli_num_rows($uc_result);

$pc_result = mysqli_query($con, $q_pwcheck);
$pc_count = mysqli_num_rows($pc_result);

if ($uc_count != 1)
{
echo "<br>invalid username<br>";
     var_dump($uc_result);
}

if ($uc_count == 1)
{

 if ($pc_count == 1)
            {
              //echo "Login Successful +code";
              session_start();                
              $_SESSION[user] = $username_l;
              header("location:member.php");
            }

    }              

else
{
    echo "<br>Invalid password for username + code<br>";
     var_dump($username_l);

    }

mysqli_close($con);

?>
<br>
<a href = "index.htm">index</a>;

1 个答案:

答案 0 :(得分:0)

我想你可能会失踪

session_start(); 

将它添加到您的代码中并尝试这样的事情(按此顺序):

session_start(); 
unset($_SESSION['name']); 
session_unset(); 
session_destroy(); 

要清除所有会话变量,这对我一直都有用:

session_start ();
foreach ($_SESSION as $key=>$val)
    unset($_SESSION[$key]);