2017-12-03 106 views
1

每當我運行logout.php腳本,然後再回到那個沒有被登錄,將無我還在session_destroy()沒有記錄我出去

logout.php登錄保護的網頁上

<?php 

session_start(); 
session_unset(); 
session_destroy(); 
header("Location: ../index.php"); 
exit(); 
?> 

的login.php

$userlogin = user_login($email, $password.$salt); 
    if ($userlogin==false){ 
     $errors[]='Wrong email/password combination.'; 
    } else { 
    //set the user session 
     $_SESSION['UserId']=$userlogin; 
     $_SESSION['LoginIP']=$_SERVER['REMOTE_ADDR']; 
     $db->query("UPDATE users SET ipadd='".$_SERVER['REMOTE_ADDR']."' WHERE user_id=".$_SESSION['UserId'].""); 
     echo '<meta http-equiv="refresh" content="0; URL=index.php">'; 

檢查記錄在片段

/* Check if user is logged in or not */ 
function loggedin(){ 
return (isset($_SESSION['UserId'])) ? true : false; 
} 
if (loggedin()==true){ 
$session_user_id = $_SESSION['UserId']; 
$user_data = user_data($session_user_id,'full_name','username'); 
$rezult =$db->query("SELECT ipadd FROM users WHERE user_id=".$_SESSION['UserId'].""); 
while($rez = $rezult->fetch_assoc()){ 
    if ($rez['ipadd']==$_SERVER['REMOTE_ADDR']) { 
    } else { 
    echo '<meta http-equiv="refresh" content="0; URL=logout2.php">'; 
    } 

} 
} 

一直在看同一問題的帖子,但無論我嘗試仍然得到相同的問題。任何建議將非常感激!

回答

-1

我總是喜歡破壞服務器會話和客戶端的cookie,如有任何錯誤,請嘗試手動覆蓋所有選項。

你可以摧毀PHP餅乾:

setcookie(session_name(), '', time() - 3600, $cookie_path, $cookie_domain, $cookie_secure, $cookie_httponly); 


<?php 
$cookie_path = "..."; 
$cookie_domain = "..."; 
$cookie_secure = "..."; 
$cookie_httponly = "..."; 

session_start(); 
session_unset(); 
session_destroy(); 
setcookie(session_name(), '', time() - 3600, $cookie_path, $cookie_domain,$cookie_secure, $cookie_httponly); 
header("Location: ../index.php"); 
exit(); 

time() - 3600使得當前的時間,這使得它無效之前Cookie的到期時間。

調查的另一個選項是登出頁面上的session_regenerate_id()。有些參考頁面下方:

php.net - session-regenerate-id

https://stackoverflow.com/a/22965580/1246494

0

我想在你的index.php文件應該有這些行:

if(!isset($_SESSION["session_name"])){ 
    header("Location: somewhere_mainpage.php"); 
} 

這是更好地使所有網頁有這些線。如果沒有會話開始,這些行將發送標題到另一個頁面。

0
<?php 
session_unset(); 
session_destroy(); 
header("Location: ../index.php"); 
?> 

應該工作,否則你可能會取消設置值

<?php 
unset($_SESSION['UserId']); // Unsets the UserId Variable reuse for each variable 
session_destroy(); 
header("Location: ../index.php"); 
?> 

你嘗試過session_destroy()?

也是我不知道無論您需要在session_start()當你在會議閉幕,從內存中你只需要啓動會議

0

我相信session_start();函數調用應該是你的登錄頁面上,當用戶登錄數據是正確的,並在註銷PHP代碼,你應該設置

session_destroy();unset($_SESSION['UserId'];

註銷。php:

<?php 
    session_destroy(); 
    /* * OR * */ 
    //unset($_SESSION['UserId']; 
    header("Location: ../index.php"); 
    exit(); 
    ?> 
+1

我同意。 'session_start();'應該在每一頁上。我只能假設OP有它,因爲'login.php'看起來像部分代碼。 – Bradmage