Home >Backend Development >PHP Tutorial >PHP operation class based on session and cookie user login status, sessioncookie_PHP tutorial
1. User login status operation class UserLogin
<?<span>php </span><span>final</span> <span>class</span><span> UserLogin { </span><span>public</span> <span>function</span><span> __construct() { } </span><span>public</span> <span>static</span> <span>function</span><span> getUserInfo() { </span><span>if</span> (<span>isset</span>(<span>$_COOKIE</span>["user_id"])&&<span>$_COOKIE</span>["user_id"]&&(<span>trim</span>(<span>$_COOKIE</span>["user_id"])!=""<span>)) { </span><span>if</span> (<span>isset</span>(<span>$_SESSION</span>["USER_INFO"<span>])) </span><span>return</span> <span>$_SESSION</span>["USER_INFO"<span>]; </span><span>$dao</span> = <span>new</span><span> UserDao(); </span><span>$user</span> = <span>$dao</span>->find(<span>$_COOKIE</span>["user_id"<span>]); </span><span>if</span> (<span>$user</span><span>) { </span><span>$_SESSION</span>["USER_INFO"] = <span>$user</span><span>; </span><span>setcookie</span>("docloud_sid", <span>session_id</span>(), <span>time</span>() + 36000<span>); </span><span>setcookie</span>("user_id", <span>$_COOKIE</span>["user_id"], <span>time</span>() + 36000<span>); </span><span>if</span> (<span>array_key_exists</span>("selected_prj_id", <span>$_COOKIE</span><span>)) </span><span>setcookie</span>("selected_prj_id", <span>$_COOKIE</span>["selected_prj_id"], <span>time</span>() + 36000<span>); </span><span>if</span> (<span>array_key_exists</span>("selected_class_id", <span>$_COOKIE</span><span>)) </span><span>setcookie</span>("selected_class_id", <span>$_COOKIE</span>["selected_class_id"], <span>time</span>() + 36000<span>); </span><span>if</span> (<span>array_key_exists</span>("selected_image_id", <span>$_COOKIE</span><span>)) </span><span>setcookie</span>("selected_image_id", <span>$_COOKIE</span>["selected_image_id"], <span>time</span>() + 36000<span>); </span><span>if</span> (<span>array_key_exists</span>("test_image_ids", <span>$_COOKIE</span><span>)) </span><span>setcookie</span>("test_image_ids", <span>$_COOKIE</span>["test_image_ids"], <span>time</span>() + 36000<span>); </span><span>if</span> (<span>array_key_exists</span>("upload_image_ids", <span>$_COOKIE</span><span>)) </span><span>setcookie</span>("upload_image_ids", <span>$_COOKIE</span>["upload_image_ids"], <span>time</span>() + 36000<span>); </span><span>return</span> <span>$user</span><span>; } } self</span>::<span>clearCookie(); </span><span>return</span> <span>null</span><span>; } </span><span>public</span> <span>static</span> <span>function</span> setUserInfo(<span>$userInfo</span><span>) { </span><span>$_SESSION</span>["USER_INFO"] = <span>$userInfo</span><span>; </span><span>setcookie</span>("docloud_sid", <span>session_id</span>(), <span>time</span>() + 36000<span>); </span><span>setcookie</span>("user_id", <span>$userInfo</span>->getId(), <span>time</span>() + 36000<span>); } </span><span>public</span> <span>static</span> <span>function</span><span> isLogin() { </span><span>if</span> (self::<span>getUserInfo()) { </span><span>return</span> <span>true</span><span>; } </span><span>return</span> <span>false</span><span>; } </span><span>public</span> <span>static</span> <span>function</span><span> delUserInfo() { self</span>::<span>clearCookie(); </span><span>session_destroy</span><span>(); } </span><span>private</span> <span>static</span> <span>function</span><span> clearCookie() { </span><span>setcookie</span>("docloud_sid", "", <span>time</span>() - 36000<span>); </span><span>setcookie</span>("user_id", "", <span>time</span>() - 36000<span>); </span><span>setcookie</span>("selected_prj_id", "", <span>time</span>() - 36000<span>); </span><span>setcookie</span>("selected_class_id", "", <span>time</span>() - 36000<span>); </span><span>setcookie</span>("selected_image_id", "", <span>time</span>() - 36000<span>); </span><span>setcookie</span>("test_image_ids", "", <span>time</span>() - 36000<span>); </span><span>setcookie</span>("upload_image_ids", "", <span>time</span>() - 36000<span>); } } </span>?>
2. Call when the user enters the username and password to make relevant judgments
<?<span>php </span><span>require_once</span> 'Init.php'<span>; </span><span>//</span><span> if logged in, logout</span> <span>if</span> (UserLogin::isLogin() && <span>$_COOKIE</span>["user_id"]==1<span>) { UserLogin</span>::<span>delUserInfo(); } </span><span>else</span> <span>if</span> (UserLogin::<span>isLogin()){ Utils</span>::redirect('welcome'<span>); } </span><span>$username</span> = <span>null</span><span>; </span><span>$password</span> = <span>null</span><span>; </span><span>$msg</span> = ""<span>; </span><span>if</span> (<span>isset</span>(<span>$_POST</span>['username']) && <span>isset</span>(<span>$_POST</span>['password'<span>])) { </span><span>$username</span> = <span>addslashes</span>(<span>trim</span>(<span>stripslashes</span>(<span>$_POST</span> ['username'<span>]))); </span><span>$password</span> = <span>addslashes</span>(<span>trim</span>(<span>stripslashes</span>(<span>$_POST</span> ['password'<span>]))); </span><span>//</span><span> validate</span> <span>$errors</span> = LoginValidator::validate(<span>$username</span>, <span>$password</span><span>); </span><span>if</span> (<span>empty</span>(<span>$errors</span><span>)) { </span><span>//</span><span> save</span> <span>$dao</span> = <span>new</span><span> UserDao(); </span><span>$user</span> = <span>$dao</span>->findByName(<span>$username</span><span>); </span><span>$last_login_ip</span> = Utils::<span>getIpAddress(); </span><span>$user</span>->setLastLoginIp(<span>$last_login_ip</span><span>); </span><span>$now</span> = <span>new</span><span> DateTime(); </span><span>$user</span>->setLastLoginTime(<span>$now</span><span>); </span><span>$dao</span>->save(<span>$user</span><span>); UserLogin</span>::setUserInfo(<span>$user</span><span>); Flash</span>::addFlash('登录成功!'<span>); Utils</span>::redirect('welcome'<span>); } </span><span>foreach</span> (<span>$errors</span> <span>as</span> <span>$e</span><span>) { </span><span>$msg</span> .= <span>$e</span>->getMessage()."<br>"<span>; } } </span>?>