PHP uses both session and cookie to save user login information Implementation code, sessioncookie_php tutorial

Source: Internet
Author: User

PHP uses both session and cookie to save the user login information implementation code, Sessioncookie


1. User Login state Operation class Userlogin

<?phpfinal class Userlogin {public Function __construct () {} public static function GetUserInfo () {if (Isset ($_co okie["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, the user entered the user name, password to make the relevant decision

<?phprequire_once ' init.php ';//If logged in, Logoutif (Userlogin::islogin () && $_cookie[ "USER_ID"]==1) {userlogin::d eluserinfo ();} else if (Userlogin::islogin ()) {Utils::redirect (' Welcome ');} $username = null, $password = NULL, $msg = "", if (isset ($_post[' username ') ") && isset ($_post[' password '])) {$userna me = 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 (' Login successful!  '); Utils::redirect (' welcome '); } foreach ($errors as $e) {$msg. = $e->getmessage (). " 
";}}?

This PHP at the same time using the session and cookie to save the user login information implementation code is small to share all the content of everyone, I hope to give you a reference, but also hope that we have a lot of support to help guests home.

http://www.bkjia.com/PHPjc/1127924.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127924.html techarticle PHP uses both session and cookie to save user login information Implementation code, Sessioncookie 1, User login state operation class Userlogin Phpfinal class Userlogin {public function __cons ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.