Home>

Enable persistent logins,When a user logs in,When "Remember me" is checked, whether or not the browser is closed,As long as you do n’t log out,Always stay logged in for a specified amount of time (the disadvantage is that after logging in on another computer,That computer ca n’t stay logged in before.)

First, persistent login is implemented using cookies, but important information such as user passwords cannot be stored in cookies.Even if encrypted.The solution is to create three new field identifiers in the user login table:the second identity,token:permanent login ID,timeout:permanent login timeout.

+ ------------ + ------------- + ------ + ----- + --------- + ---------------- +
field | type | null | key | default | extra |
+ ------------ + ------------- + ------ + ----- + --------- + ---------------- +
| uid | int (11) | no | pri | null | auto_increment |
| uname | varchar (20) | yes | | null | |
| upwd | varchar (20) | yes | | null | |
| uflag | int (11) | yes | | null | |
| identifier | varchar (32) | yes | | null | |
| token | varchar (32) | yes | | null | |
timeout | int (11) | yes | | null | |
+ ------------ + ------------- + ------ + ----- + --------- + ---------------- +

When the user selects "Remember me" to log in, a unique identifier, a unique token should be generated, and an expiration time timeout should be set, two values ​​representing the identity should be written into the cookie, and the cookie expiration time should be timeout For example:setcookie ("auth", "$identifier:$token", $timeout);Insert three values ​​into the data table at the same time;When a user visits the site again,First determine whether the cookie contains auth. If it contains,Then go to the database for identity comparison (identifier and token). When the comparison is successful,Write user information to the session while the user remains logged in.

Code:

Controller testcontroller.class.php

<?php
namespace test \ controller;
use think \ controller;
class testcontroller extends controller {
 public function login () {
  //Determine whether to log in permanently
  $this->checklong ();
  //Jump to personal center if already logged in
  if (isset ($_ session ["username"])) {
   $this->redirect ("test/ucenter");
  } else {
   //determine if a cookie exists
   if (isset ($_ cookie ["username"])) {
    $this->assign ("username", $_ cookie ["username"]);
   }
   //Show registration page
   $this->display ("test");
  }
 }
 //Show verification code
 public function verifyimg () {
  $verify=new \ think \ verify ();
  //$verify->usezh=true;//Use Chinese verification code
  $verify->length=4;
  $verify->entry ();
 }
 //Verify login
 public function check () {
  $verify=new \ think \ verify ();
  if ($verify->check (i ("yzm")))) {
   //Judging the username and password
   $user=new \ test \ model \ testmodel ();
   $res=$user->checkname (i ("username"), i ("pwd"));
   if ($res === false) {
    echo "Incorrect username or password";
   } else {
    //User information is stored in the session
    session ("username", $res ["uname"]);
    session ("id", $res ["uid"]);
    //If the user checked "Remember me", keep login permanently
    if (i ("remember")) {
     $salt=$this->random_str (16);
     //Second avatar identification
     $identifier=md5 ($salt. md5 (i ("username"). $salt));
     //Permanent login ID
     $token=md5 (uniqid (rand (), true));
     //Permanent login timeout (1 week)
     $timeout=time () + 3600 * 24 * 7;
     //deposit cookie
     setcookie ("auth", "$identifier:$token", $timeout);
     $user->saveremember ($res ["uid"], $identifier, $token, $timeout);
    }
    //Save username in cookie, save username information in form after logout
    setcookie ("username", i ("username"), time () + 3600 * 24);
    //Jump to Member Center
    $this->redirect ("test/ucenter");
   }
  } else {
   echo "Input error";
  }
 }
 //Test the strstr function
 public function strstrtest () {
  $param="think \ verify";
  //The third parameter is true and returns "think";without the third parameter,Returns "\ verify"
  $name=strstr ($param, "\\", true);
  echo $name;
 }
 //User Center
 public function ucenter () {
  //Determine whether to log in permanently
  $this->checklong ();
  $this->assign ("session", $_ session);
  $this->display ("ucenter");
 }
 //sign out
 public function loginout () {
  session (null);
  setcookie ("auth", "", time ()-1);
  $this->redirect ("test/login");
 }
 //Generate a random number,Used to generate salt
 public function random_str ($length) {
  //Generate a letter containing uppercase English letters, Lowercase english letters, Array of numbers
  $arr=array_merge (range (0, 9), range ("a", "z"), range ("a", "z"));
  $str="";
  $arr_len=count ($arr);
  for ($i=0;$i<$length;$i ++) {
   $rand=mt_rand (0, $arr_len-1);
   $str.=$arr [$rand];
  }
  return $str;
 }
 //Determine whether to log in permanently
 public function checklong () {
  $check=new \ test \ model \ testmodel ();
  $is_long=$check->checkremember ();
  if ($is_long === false) {
  } else {
   session ("username", $is_long ["uname"]);
   session ("id", $is_long ["uid"]);
  }
 }
}

Model testmodel.class.php

<?php
namespace test \ model;
use think \ model;
class testmodel extends model {
 //Verify login information
 public function checkname ($name, $pwd) {
  $admin=m ("admin");
  $info=$admin->getbyuname ($name);
  if ($info!=null) {
   //verify password
   if ($info ["upwd"] == $pwd) {
    return $info;
   } else {
    return false;
   }
  } else {
   return false;
  }
 }
 //When the user ticks "Remember me"
 public function saveremember ($uid, $identifier, $token, $timeout) {
  $admin=m ("admin");
  $data ["identifier"]=$identifier;
  $data ["token"]=$token;
  $data ["timeout"]=$timeout;
  $where="uid =". $uid;
  $res=$admin->data ($data)->where ($where)->save ();
  return $res;
 }
 //Verify if the user is logged in permanently (remember me)
 public function checkremember () {
  $arr=array ();
  $now=time ();
  list ($identifier, $token)=explode (":", $_ cookie ["auth"]);
  if (ctype_alnum ($identifier) ​​&&ctype_alnum ($token)) {
   $arr ["identifier"]=$identifier;
   $arr ["token"]=$token;
  } else {
   return false;
  }
  $admin=m ("admin");
  $info=$admin->getbyidentifier ($arr ["identifier"]);
  if ($info!=null) {
   if ($arr ["token"]!=$info ["token"]) {
    return false;
   } else if ($now>$info ["timeout"]) {
    return false;
   } else {
    return $info;
   }
  } else {
   return false;
  }
 }
}

View login page test.html

<doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>document</title>
</head>
<body>
<form action="__ controller __/check" method="post">
<if condition="$username neq null">
 <input type="text" name="username" placeholder="username" value="{$username}"<br>
<else />
 <input type="text" name="username" placeholder="username"><br>
</if>
<input type="password" name="pwd" placeholder="password"<br>
<input type="text" name="yzm" placeholder="Verification Code"<img src="__ controller __/verifyimg" onclick="this.src=this.src+"?"+ math. random () "<br>
<input type="checkbox" name="remember"<label for="remember">remember me</label>
<input type="submit" value="submit">
</form>
</body>
</html>

View Personal Center ucenter.html

<doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>documenttitle>
</head>
<body>
 <if condition="$session [" username "] neq null">
 <i>{$session.username},</i>
 <else />
 <i>tourists,</i>
 </if>
 Welcome<br>
 <a href="__ controller __/loginout">Logout</a>
</body>
</html>

Attachment:module catalog

php
  • Previous jQuery simple method to implement countdown based on extension
  • Next Forty-eighth of Manipulating Data in ASPNET 20: Using Open Concurrency for the SqlDataSource Control