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
Check if the user is logged in
The code is as follows |
Copy Code |
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 form 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 data if (empty ($user)) {//username password not to fetch information, go to login page Header ("location:login.php?req_url=". $_server[' Request_uri '); }else{ $_session[' user_info '] = $user; Username and password Right, put the user's personal data into the session } } } } |
Second, the user submits the login information
code as follows |
copy code |
u Sername = 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 user name and password cannot be empty"; }else{ $row = GetUserInfo ($username, $password); if (empty ($row)) { $err _msg = "username and password are incorrect"; }else{ $_session[' user_info '] = $row; if (!empty ($remember)) {//If the user chooses, log the login status and put the username and 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
Sign Out
The code is as follows |
Copy Code |
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); } } |
http://www.bkjia.com/PHPjc/444728.html www.bkjia.com true http://www.bkjia.com/PHPjc/444728.html techarticle 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 ...