ThinkPHP3.2.2 how to implement the persistent login (remember me) function, thinkphp3.2.2
This article describes how ThinkPHP3.2.2 implements the persistent login function. We will share this with you for your reference. The details are as follows:
Enable persistent logon. When a user logs on, after checking "Remember Me", no matter whether the browser is closed or not, as long as the user does not exit the logon, the logon status is always maintained at the specified time (the disadvantage is that after logging on to another computer, the previous computer cannot continue to log on ).
First, persistent login is implemented using cookies, but Cookies cannot store important information such as user passwords, even if they are encrypted. The solution is to create three fields in the User Logon table: identifier: second identity, token: Permanent logon identity, timeout: Permanent logon 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 you select "remember me" for logon, you should generate a unique identifier, a unique token, and set an expiration time timeout to write two values representing identities into cookies, set the cookie expiration time to timeout, for example, setcookie ('auth', "$ identifier: $ token", $ timeout), and insert the three values into the data table; when the user visits the website again, he first checks whether the cookie contains auth. If the cookie contains auth, the user goes to the database for identity comparison (identifier and token). When the comparison is successful, write user information into the session and keep the user logged on.
Code:
Controller TestController. class. php
<? Phpnamespace Test \ Controller; use Think \ Controller; class TestController extends Controller {public function login () {// determine whether to log on permanently $ this-> checkLong (); // if you have logged on, go to the personal center. if (isset ($ _ SESSION ['username']) {$ this-> redirect ('test/ucenter ');} else {// determine whether a cookie exists. if (isset ($ _ COOKIE ['username']) {$ this-> assign ('username ', $ _ COOKIE ['username']);} // display the registration page $ this-> display ("test") ;}// display the verification code public function verifyImg () {$ verify = new \ Think \ Verify (); // $ verify-> useZh = true; // use the Chinese Verification Code $ verify-> length = 4; $ verify-> entry ();} // verify logon to public function check () {$ Verify = new \ Think \ verify (); if ($ verify-> check (I ("yzm") {// determine the user name and password $ user = new \ Test \ Model \ TestModel (); $ res = $ user-> checkName (I ("username"), I ("pwd"); if ($ res = false) {echo "incorrect user name or password";} else {// The user information is stored in the session ("username", $ res ['uname']); session ("id ", $ res ['uid']); // if the user selects "remember Me", keep the persistent login if (I ("remember ")) {$ salt = $ this-> random_str (16); // The Second ID $ identifier = md5 ($ salt. md5 (I ("username "). $ salt); // permanent logon ID $ token = md5 (uniqid (rand (), true); // permanent logon timeout (1 week) $ timeout = time () + 3600*24*7; // Save the cookie setcookie ('auth', "$ identifier: $ token", $ timeout ); $ user-> saveRemember ($ res ['uid'], $ identifier, $ token, $ timeout);} // saves the user name to the cookie, after logging out, save the username information in the form setcookie ('username', I ('username'), time () + 3600*24 ); // jump to member center $ this-> redirect ('test/ucenter') ;}} else {echo "input error ";}} // test strstr function public function strstrstrtest () {$ param = "Think \ Verify"; // if the third parameter is true, 'think' is returned. No third parameter exists, return '\ Verify' $ name = strstr ($ param, '\', true); echo $ name;} // user center public function ucenter () {// determine whether to log on permanently $ this-> checkLong (); $ this-> assign ("session", $ _ SESSION ); $ this-> display ("ucenter");} // log out of the public function loginout () {session (null); setcookie ('auth', '', time () -1); $ this-> redirect ("Test/login");} // generates a random number to generate the salt public function random_str ($ length) {// generate an array containing uppercase English letters, lowercase English letters, and 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 persistently log on to the 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
<? Phpnamespace Test \ Model; use Think \ Model; class TestModel extends Model {// verify the logon information public function checkName ($ name, $ pwd) {$ admin = M ("admin"); $ info = $ admin-> getByUname ($ name); if ($ info! = Null) {// verify the password if ($ info ['upwd '] ==$ pwd) {return $ info ;}else {return false ;}} else {return false ;}// when the user selects "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 that the user has logged on 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 logon page test.html
<DOCTYPE html>
View personal center ucenter.html
<DOCTYPE html>
Appendix: Module Directory
Supplement: The editor recommends a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
Php code online formatting and beautification tools:Http://tools.jb51.net/code/phpformat