2 つのオプション: 1 つは、ユーザー ID と最終アクセスの連想配列を作成し、それを Cookie のコンテンツとしてシリアル化して保存することです。2 番目のオプションは、ユーザーごとに「last_visit_[user_id]」という Cookie を作成することです。最初のものはおそらくより使いやすく、必要なユーザー Cookie が少なくて済みます。
編集 - これは未テストの例です。エラーが発生した場合は申し訳ありませんが、これで要点がわかります。
重要なデータは、$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, '/');