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
Copy CodeThe 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 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
}
}
}
}
Two, the user submits the login information
Copy CodeThe 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 verification code";
}elseif ($username = = ' | | $password = = ') {
$err _msg = "User name and password cannot be empty";
}else{
$row = GetUserInfo ($username, $password);
if (empty ($row)) {
$err _msg = "username 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
Copy CodeThe code is as follows:
Sign Out
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
Copy the Code code as follows:
<?
The value of the user name and password to read the cookie can be
if ($_cookie[' uname ']!= ') {$CKUNAME = $_cookie[' uname '];}
if ($_cookie[' pwd ']!= ') {$CKPWD = $_cookie[' pwd '];}
Echo $CKUNAME;
Echo '
';
Echo $CKPWD;
?>
<?
Log in to save your username and password to a cookie
if ($_post[' button ']!= ') {
$uname = $_post[' uname ');
$pwd = $_post[' pwd '];
If the encryption password entered is not the same as in the cookie, then the encryption
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);
}
}
?>