简体   繁体   中英

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

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

But when people login, this does not happen immediately due to some reason. The same with the Logout script: It works, but not immediately. I have to try about 2-4 times before something happens.

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. 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?

Replace all session-related functions in logout.php with these lines:

session_start();
session_destroy();

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