This article mainly describes the PHP use of cookies to remember the login status, this article with the most primitive way to explain how to realize the login status, give 3 steps and specific implementation code, the need for friends can refer to the next
To realize the ability to remember password automatic login Most of us are using the client's cookies to achieve, we use PHP is no exception, the need for friends can refer to.
PHP Production Remember password automatic login to solve the idea, in fact, is the operation of Session,cookies
First, check whether the user is logged in
The code is as follows:
Check if the user is logged in function Checklogin () { if (empty ($_session[' user_info ')) { //check to see if the SESSION is empty if (empty ($_ cookie[' username ') | | Empty ($_cookie[' password ')) { //if session is empty and the user does not have the option to log the login header ("Location:login.php?req_url=". $_ server[' Request_uri '); Go to the login page, log the URL of the request, log in and jump past, and the user experience is good. }else{ //user selected Remember login status $user = GetUserInfo ($_cookie[' username '],$_cookie[' password ')); To fetch the user's personal information if (empty ($user)) { //username password not to fetch information, go to the login page header ("Location:login.php?req_url=". $_ server[' Request_uri '); } else{ $_session[' user_info '] = $user; User name and password pair, put the user's profile inside the session}} }
Second, the user submits the login information
The code is as follows:
Username = Trim ($_post[' username '); $password = MD5 (Trim ($_post[' password ')); $validatecode = $_post[' Validatecode '); $ref _url = $_get[' Req_url '); $remember = $_post[' remember '); $err _msg = "; if ($validatecode!=$_session[' checksum ') { $err _msg = "Incorrect Captcha"; } ElseIf ($username = = ' | | $password = = ') { $err _msg = "The username and password cannot be empty"; } else{ $row = GetUserInfo ($username, $password); if (empty ($row)) { $err _msg = "The user name and password are not correct"; } else{ $_session[' user_info '] = $row; if (!empty ($remember)) { //If the user chooses, log the login status and put the username and secret password into the cookie Setcookie ("username", $username, Time () + 3600*24*365); Setcookie ("Password", $password, Time () +3600*24*365); } if (Strpos ($ref _url, "login.php") = = = False) { header ("Location:". $ref _url); } else{ Header ("location:main_user.php");}}}
Third, when the user points out, clear record log in status
The code is as follows:
Exit Login function Logout () { unset ($_session[' user_info '); if (!empty ($_cookie[' username ')) | |!empty ($_cookie[' password ')) { Setcookie ("username", NULL, Time () -3600*24* 365); Setcookie ("password", NULL, Time () -3600*24*365); } }
Four, concise version of the example
The code is as follows:
<?//the value of the user name and password of the COOKIE can be read if ($_cookie[' uname ']!= ') {$CKUNAME = $_cookie[' uname '];} if ($_cookie[' pwd ']!= ') {$CKPWD = $_cookie[' pwd '];} echo $CKUNAME; Echo ' <br> '; echo $CKPWD;? ><form id= "Form1" Name= "Form1" method= "Post" action= "" > <input type= "text" name= "uname" id= "uname" value= "& lt;? = $CKUNAME;? > "/> <input type=" password "name=" pwd "id=" pwd "value=" <?= $CKPWD;? > "/> <input name=" Remember "type=" checkbox "value=" 1 "<? if ($CKUNAME! = ") {?> checked=" checked "<?}?>/> Remember me! <input type= "Submit" name= "button" id= "button" value= "Login"/></form><?//login, save username and password to Cookieif ($_post[ ' button ']!= ') {$uname = $_post[' uname '); $pwd = $_post[' pwd ']; If the encryption password entered is not the same as in the cookie, then encrypt if ($pwd! = $CKPWD) {$pwd = MD5 ($PWD);} $remember = $_post[' remember '); if ($remember ==1) {Setcookie ("uname", $uname, Time () +3600*24*30); Setcookie ("pwd", $pwd, Time () +3600*24*30); }}?>