您好,登錄后才能下訂單哦!
如何在php中利用session與cookie保存用戶的登錄信息?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1、用戶登錄狀態操作類UserLogin
<?php final class UserLogin { public function __construct() { } public static function getUserInfo() { if (isset($_COOKIE["user_id"])&&$_COOKIE["user_id"]&&(trim($_COOKIE["user_id"])!="")) { if (isset($_SESSION["USER_INFO"])) return $_SESSION["USER_INFO"]; $dao = new UserDao(); $user = $dao->find($_COOKIE["user_id"]); if ($user) { $_SESSION["USER_INFO"] = $user; setcookie("docloud_sid", session_id(), time() + 36000); setcookie("user_id", $_COOKIE["user_id"], time() + 36000); if (array_key_exists("selected_prj_id", $_COOKIE)) setcookie("selected_prj_id", $_COOKIE["selected_prj_id"], time() + 36000); if (array_key_exists("selected_class_id", $_COOKIE)) setcookie("selected_class_id", $_COOKIE["selected_class_id"], time() + 36000); if (array_key_exists("selected_image_id", $_COOKIE)) setcookie("selected_image_id", $_COOKIE["selected_image_id"], time() + 36000); if (array_key_exists("test_image_ids", $_COOKIE)) setcookie("test_image_ids", $_COOKIE["test_image_ids"], time() + 36000); if (array_key_exists("upload_image_ids", $_COOKIE)) setcookie("upload_image_ids", $_COOKIE["upload_image_ids"], time() + 36000); return $user; } } self::clearCookie(); return null; } public static function setUserInfo($userInfo) { $_SESSION["USER_INFO"] = $userInfo; setcookie("docloud_sid", session_id(), time() + 36000); setcookie("user_id", $userInfo->getId(), time() + 36000); } public static function isLogin() { if (self::getUserInfo()) { return true; } return false; } public static function delUserInfo() { self::clearCookie(); session_destroy(); } private static function clearCookie() { setcookie("docloud_sid", "", time() - 36000); setcookie("user_id", "", time() - 36000); setcookie("selected_prj_id", "", time() - 36000); setcookie("selected_class_id", "", time() - 36000); setcookie("selected_image_id", "", time() - 36000); setcookie("test_image_ids", "", time() - 36000); setcookie("upload_image_ids", "", time() - 36000); } } ?>
2、在用戶輸入用戶名、密碼處調用來做相關判定
<?php require_once 'Init.php'; // if logged in, logout if (UserLogin::isLogin() && $_COOKIE["user_id"]==1) { UserLogin::delUserInfo(); } else if (UserLogin::isLogin()){ Utils::redirect('welcome'); } $username = null; $password = null; $msg = ""; if (isset($_POST['username']) && isset($_POST['password'])) { $username = addslashes(trim(stripslashes($_POST ['username']))); $password = addslashes(trim(stripslashes($_POST ['password']))); // validate $errors = LoginValidator::validate($username, $password); if (empty($errors)) { // save $dao = new UserDao(); $user = $dao->findByName($username); $last_login_ip = Utils::getIpAddress(); $user->setLastLoginIp($last_login_ip); $now = new DateTime(); $user->setLastLoginTime($now); $dao->save($user); UserLogin::setUserInfo($user); Flash::addFlash('登錄成功!'); Utils::redirect('welcome'); } foreach ($errors as $e) { $msg .= $e->getMessage()."<br>"; } } ?>
關于如何在php中利用session與cookie保存用戶的登錄信息問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。