PHP Logout won'当用户注销时,将用户从用户所在的页面注销


PHP Logout won't log user out from the page the user was on, when they logged out

我有一个PHP网站登录和注销,使用$_SESSION['userName']来存储登录成员的username

但是当人们登录时,由于某些原因,这不会立即发生。Logout脚本也是如此:它可以工作,但不是立即工作。我得试2-4次才会有结果。

这是我的登录代码和注销代码:

代码:/login。

session_start();
//=============Configuring Server and Database=======
$host        =    'host';
$user        =    'username';
$password    =    'password';
//=============Data Base Information=================
$database    =    'database';
$conn        =    mysql_connect($host,$user,$password) or die('Server Information 
is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');
//===============End Server Configuration============
//*******Form Information********
$userName=mysql_real_escape_string($_POST['username']); 
$password=mysql_real_escape_string($_POST['password']); 
$passWord=md5($password); // Encrypted Password
//*********retrieving data from Database**********
$query = "select * from users where userName='$userName' and passWord='$passWord'";
$res = mysql_query($query);
$rows = mysql_num_rows($res);
//**********if $userName and $passWord will match database, The above function 
//**********will return 1 row
if($rows==1)
//***if the userName and password matches then register a session and redrect 
//***user to the Successfull.php
{
    $_SESSION['userName'] = $userName;
    header("location: ../index.php");
}
else
{
    echo 'Incorrect username or password.';
}
exit;

代码:/logout.php

session_name('userName');
session_start('userName');
session_unset('userName');
session_destroy();
header("Location:index.php");
我真的希望你能帮我解决这个问题。登录工作,注销可以将用户注销所有页面,除了用户所在的页面,当他们点击"注销"…什么好主意吗?

将logout.php中所有与会话相关的函数替换为以下行:

session_start();
session_destroy();