为啥不会/如何对 PHP 会话变量进行排序而不是取消设置
Posted
技术标签:
【中文标题】为啥不会/如何对 PHP 会话变量进行排序而不是取消设置【英文标题】:Why won't/How can sort PHP session variables not unsetting为什么不会/如何对 PHP 会话变量进行排序而不是取消设置 【发布时间】:2014-02-13 13:52:24 【问题描述】:似乎在我所有的尝试中,会话变量在被指示时都没有被取消设置。我试过的方法是:
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 但不知道如何直接操作/删除它,甚至不确定这是否是解决方案(尽管我有唯一的想法)。
完整代码..
索引.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(其中会话变量是 unsest 等):
<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(会话变量被转储的地方):
<?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>
登录.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>;
【问题讨论】:
浏览器缓存与会话变量无关,它们在服务器上。 提供unset
s 变量和var_dump
s 变量的完整代码
顶部的所有东西都应该工作。请张贴SSCCE 说明问题。
session_destroy 足以摧毁整个会话。可能还有其他原因导致问题。如果您共享完整的代码和输出会更好
好的,我已经添加了来自各种相关脚本的所有代码..
【参考方案1】:
我想你可能错过了
session_start();
将其添加到您的代码中并尝试这样的事情(按此顺序):
session_start();
unset($_SESSION['name']);
session_unset();
session_destroy();
要清除所有会话变量,这对我一直有效:
session_start ();
foreach ($_SESSION as $key=>$val)
unset($_SESSION[$key]);
【讨论】:
以上是关于为啥不会/如何对 PHP 会话变量进行排序而不是取消设置的主要内容,如果未能解决你的问题,请参考以下文章