2012-06-15 20 views
0

你好,如何閱讀multiliple會員餅乾在PHP

如何設置像「LAST_VISIT」餅乾,如果用戶對我的網站 我嘗試了一些方法,但我失敗了多個帳戶。

that`s代碼,當用戶註銷

setcookie('last_visit',time(),time()+60*60*24*30*12); 

但是當用戶用另一個賬戶重新登錄頁面讀他的第一個客戶的最後一次訪問,此代碼

$lastvisit = $_COOKIE['last_visit']; 

能我鏈接上次訪問Cookie與用戶ID cookie在一個cookie

請幫我用正確的方法

回答

0

嘗試刪除現有的或舊的cookies時,寫入新的。做一個互斥的登錄,當用戶登錄到一個賬戶時,他們將從其他賬戶註銷。

+0

是的,我知道,但是當用戶登出的網站放一個NULL值,因爲這種餅乾有值,當他們登錄的,但是,當他們離開that's lastvisit值必須是刪除其cookie一樣USER_NAME USER_ID區別。所以現在我有最後一次訪問的價值,當用戶使用另一個帳戶登錄時,該網站將讀取他登錄的第一個帳戶的最後訪問cookie,所以我必須將此cookie與另一個值(如用戶標識)區分開來,但我不知道該方法 –

0

兩種選擇:一種是創建一個用戶ID和上次訪問的關聯數組,並存儲序列化爲cookie的內容。第二種選擇是爲每個用戶創建一個名爲「last_visit_ [user_id]」的cookie。首先可能更友好添加它需要更少的用戶cookie。


編輯 - 這是一個UNTESTED示例。對不起,任何錯誤,但這應該給你的主旨。

關鍵數據存儲在$ aLastLogged [UserID] = TimeLastLogged的關聯數組中;

// This will be the user id of hte current user as set somewhere else. 
$userID = 123; 


// This will store the time the user last logged on. 0 = not logged on 
$LastLoggedOn = 0; 

// Read the cookie - allow for errors! 
$aLastLogged = false; // Start with "false" and we'll correct later if is works 
// Check if cookie exists 
if (isset($_COOKIE['last_logged'])) { 
    // Read the cookie contents. The @ hides the errors in case the cookie is corrupted 
    $aLastLogged = @unserialize($_COOKIE['last_logged']); 
} 

// At this point, aLastLogged will be an array, or false if it was not set or failed 

if ($aLastLogged) { 
    // See if this user has been here before by checking if there is an element in the array 
    if (isset($aLastLogged[$userID])) { 
     $LastLoggedOn = (int)$aLastLogged[$userID]; 
     // Note - I cast as "int" to check it's a valid number. You could alos check hte range is valid - just in case someone's been fiddlign with the cookie. Trust no input, ever, including cookies 
    } 
} else { 
    // Create a blank array for the cookie 
    $aLastLogged = array(); 
} 


// At this point, you have the time in $LastLoggedOn 

// Now update the cookie by creating/updating hte associative element 
$aLastLogged[$user_ID] = time(); 
// Set the cookie; set the time and path and domain as required - the following is for 1 day - you'll want longer. 
setcookie('last_logged', serialize($aLastLogged), time() + 86400, '/'); 
+0

感謝您的重播,但我可以給你一個例子。 –

+0

答案舉一個例子。它沒有經過測試 - 對於任何錯誤都很抱歉 - 但它應該足以讓您瞭解正在發生的事情並據此進行修改。 – Robbie