【问题标题】:The session_destroy(); doesn't work correctly on server with php 5.3.21session_destroy();在使用 php 5.3.21 的服务器上无法正常工作
【发布时间】:2013-03-12 15:55:08
【问题描述】:

抱歉,如果这是一个冗长的帖子,但我需要问这个问题。好吧,我已经在几台服务器上尝试过这段代码,但它不能正常工作!

我一直在尝试将我的 PHP 脚本从 mysql 更改为 mysqli,到目前为止,失败多于成功。

注册表工作正常,它将记录添加到 mysql 数据库,它也会向用户发送一封电子邮件,没有任何错误。我在所有页面上都有这个,以确保我没有收到任何错误:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?> 

登录表单也可以正常工作,它会将用户登录到他们的帐户。

但这就是问题所在,还有几个我一直面临的问题。

1- 页面顶部有一个简单的顶部链接,如果用户未登录,它将显示登录和注册,一旦用户登录,它将显示他们的用户名和注销链接。这似乎有它的想法拥有,因为它可以在一台服务器上运行,而不能在另一台服务器上运行。通过工作,我的意思是即使用户已登录,它也会在一台服务器上保持登录和注册链接。

2- 有一个 logout.php 文件应该注销用户并结束会话。但这又有了自己的想法。在较旧的 PHP 服务器上,它可以正常工作,但如果我再次刷新用户帐户 URL 上的页面,它会自动将用户重新登录。我在哪个浏览器中尝试以及清除缓存的次数都没有关系和饼干。它仍然会在页面刷新时将用户重新登录到帐户中。

另外,logout.php 文件在 php 版本 5.3.21 的服务器上不起作用,它不会让用户退出他们的帐户!!

这是 member.php 代码:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
session_start(); // Must start session first thing
// See if they are a logged in member by checking Session data
$toplinks = "";
if (isset($_SESSION['id'])) {
    // Put stored session variables into local php variable
    $userid = $_SESSION['id'];
    $username = $_SESSION['username'];
    $toplinks = '<a href="member.php?id=' . $userid . '">' . $username . '</a> &bull; 
    <a href="member.php">Account</a> &bull; 
    <a href="logout.php">Log Out</a>';
} else {
    $toplinks = '<a href="join_form.php">Register</a> &bull; <a href="login.php">Login</a>';
}
?>
<?php
// Use the URL 'id' variable to set who we want to query info about
$id = preg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for security
if ($id == "") {
    echo "Missing Data to Run";
    exit();
}
//Connect to the database through our include 
include_once "config/connect.php";
// Query member data from the database and ready it for display
$sql = "SELECT * FROM members WHERE id='$id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$count = mysqli_num_rows($query);
if ($count > 1) {
    echo "There is no user with that id here.";
    exit(); 
}
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$username = $row["username"];
$_SESSION['username'] = $username;
$userid = $row["id"];
$_SESSION['id'] = $userid;
// Convert the sign up date to be more readable by humans
$signupdate = strftime("%b %d, %Y", strtotime($row['signupdate']));
}
?>

这是 logout.php 文件:

<?php
session_start(); 
session_destroy();
if( isset($_SESSION['id'])){ 
header("location: index.php");
} else {
exit('<h2>Could not log you out, sorry the system encountered an error.</h2>');
} 
?> 
<html>
<body>
<?php echo "$msg"; ?><br>
<p><a href="index.php">Click here</a> to return to our home page </p>
</body>
</html>

任何帮助将不胜感激。

【问题讨论】:

标签: php mysql


【解决方案1】:

session_destroy() 不会破坏会话变量。您可以使用“$_SESSION = array();”如果你想这样做。 最好的问候

【讨论】:

  • 啊,好吧...也许你可以试试这个帖子的解决方案:stackoverflow.com/questions/3989347/…
  • 谢谢,$params["path"], $params["domain"], $params["secure"], $params["httponly"] 有什么用?还是我需要将它们保留为默认值?
  • 不客气。 “$params = session_get_cookie_params();”应该是正确的,你不必改变它......
  • 能否在“$_SESSION = array();”之后显示会话变量("var_dump($_SESSION);")? (可能会在之后再次创建会话。)
  • 这是我放置 var_dump($_SESSION); 时得到的在“$_SESSION = array();”之后: array(0) { } 警告:无法修改标头信息 - 标头已由第 16 行 /logout.php 中的(输出开始于 /logout.php:8)发送
【解决方案2】:

试试:

session_destroy();
session_write_close();

【讨论】:

    猜你喜欢
    • 2012-12-16
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2012-08-02
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多