2011-05-03 71 views
0

我想要一個按鈕在我的網站上讀取重置,當點擊會話重新設置?有任何想法嗎?我的意思是重新設置只是清除會話,所以我想要一個html按鈕,點擊時只是清除會話。PHP會話重置按鈕

+0

重置意味着什麼? (保持會話進行,清除會話,...?) – 2011-05-03 18:26:49

回答

1

此按鈕將提交表單的PHP腳本,這是否:

session_start(); 
session_destroy(); 

// Typically, after doing this you will redirect the user 
// to the home page with something like: 
header('Location: index.php'); 

這一切就是這麼簡單。

不要被「提交表單」部分混淆 - 您不需要需要除了按鈕之外的任何形式。

+0

或者只是'session_start(); $ _SESSION = array()'。 – 2011-05-03 18:29:04

+0

但是這實際上並沒有*重置*會話,它只會破壞當前會話? – 2011-05-03 18:30:41

+0

@MarcB,@Crayon:銷燬會話後,習慣上按照PRG模式重定向用戶。當新頁面加載時,會話將不再存在,因此不需要顯式取消設置變量等。 – Jon 2011-05-03 18:33:18

1

簡單的方法(這幾乎是100%安全的): 使用JavaScript來刪除的cookie(大部分時間PHPSESSID的)

位更復雜的方式(這是更爲安全的): 使用PHP來取消設置$ _SESSION(所以用戶不能再對PHPSESSID做任何事情,以防止劫持),然後取消設置或重新生成$ _COOKIE ['PHPSESSID'](或session_regenerate_id)。

1

創建一個包含兩件事情的一種形式: 1)包含當前URL 2)一個按鈕,按下時,將用戶定向到掃清第二會話

頁隱藏變種頁面中,清除會話,然後使用表單中的$ _POST信息將用戶重定向到它們所在的頁面。

摧毀會議:

session_start(); // if it's not already started. 
session_unset(); 
session_destroy(); 
1

這是從PHP手冊session_destroy:

<?php 
// Initialize the session. 
// If you are using session_name("something"), don't forget it now! 
session_start(); 

// Unset all of the session variables. 
$_SESSION = array(); 

// If it's desired to kill the session, also delete the session cookie. 
// Note: This will destroy the session, and not just the session data! 
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
     $params["path"], $params["domain"], 
     $params["secure"], $params["httponly"] 
    ); 
} 

// Finally, destroy the session. 
session_destroy(); 
?> 

這第一重置所有會話數據(所以以後不能在相同的使用頁面加載),然後刪除會話cookie並最終銷燬會從磁盤擦除數據文件的會話。