PHP also uses session and cookies to save the implementation code _php instance of user login information

Source: Internet
Author: User
Tags setcookie

1, User logon Status action class 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;
  The 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, user input user name, password to make the relevant decision

<?php require_once ' init.php ';  If logged in, logout if (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 '])) {$username = Addslashes (Trim (stripslashes
 [' 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 ().
 <br> "; }}?>

This PHP at the same time using session and cookies to save user login information is the implementation of the code is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.