0

我有一个带有登录和注销的 PHP 站点,$_SESSION['userName']用于存储username已登录成员的登录信息。

但是当人们登录时,由于某种原因,这不会立即发生。注销脚本也是如此:它可以工作,但不能立即生效。在发生某些事情之前,我必须尝试大约 2-4 次。

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

代码:/login.php

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");

我真的希望你能帮助我解决这个问题。

编辑1:好的,现在登录工作,注销现在可以将用户从所有页面中注销,除了用户所在的页面,当他们点击“注销”时......有什么想法吗?

4

2 回答 2

0

在 PHP 中,每当您需要页面上的会话变量时。您必须先在同一页面上开始会话。

通过添加

session_start();

在向浏览器输出任何消息或字符之前,否则它将显示警告消息。

让我们来看看后面的注销功能。你应该在哪里使用 session_destroy(); 杀死所有会话。

于 2013-06-18T12:19:27.650 回答
0

a)也许浏览器缓存正在工作,请在执行任何操作之前尝试添加以下说明:

header("Pragma: no-cache");
header("Cache-Control: no-cache");

b) 注意:session_start 似乎没有任何参数支持

于 2013-06-18T12:20:50.777 回答