2013-06-18 72 views
0

我有一个PHP的网站与登录与注销,使用$_SESSION['userName']存储登录的成员的usernamePHP会话登录和注销未能立即着手

但是,当人们登录时,由于某种原因,这不会立即发生。与注销脚本相同:它可以工作,但不会立即生效。在事情发生之前,我必须尝试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:好吧现在登录工作,登出现在可以登出所有页面的用户出现用户在哪里,当他们点击“登出”的页面...任何想法?

+0

试试这个您注销文件:session_start(); session_destroy();标题( “位置:的index.php”); –

回答

0

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

加入

session_start();

之前的任何输出消息或字符到浏览器,否则它会显示一条警告消息。

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

0

一)也许是浏览器的缓存工作,尝试做任何事情之前添加以下说明:

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

二)通知:在session_start似乎没有任何参数支持的

+0

我很高兴你的家伙正在尝试帮助,但用户仍然dosn't注销:( – user2372837