简体   繁体   中英

php logout fails to log user out of the page they logged out from

I have a PHP site with Login and Logout, using $_SESSION['userName'] to store the username of the logged in member.

The login works, and the logout can log the user out of all pages EXEPT the page the user where on, when they clicked "logout" ... Any ideas?

Here is my Login code and Logout code:

Code: /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;

Code: /logout.php

session_name('userName');
session_start('userName');
session_unset('userName');
session_destroy();
header("Location:index.php");

I really hope you can help me with this issue.

i am assuming you have a bit of code in your script to check username session is set.

session_start();
$_SESSION['userName'] ='';
session_unset();
session_destroy();
header("Location: index.php");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM