PHP uses cookies to implement remember login status _php instance

Source: Internet
Author: User
Tags md5 setcookie

To realize the ability to remember the automatic login of the password most of us are using the client's cookies to achieve, we use PHP is no exception, the need for friends can refer to it.

PHP Production Remember the password automatic login solution idea, in fact, is the operation of the Session,cookies

First, check whether the user is logged in

Copy Code code as follows:

Check if the user is logged on
function Checklogin () {
if (Empty ($_session[' user_info ')) {//check if the session is empty
if (Empty ($_cookie[' username ')) | | | empty ($_cookie[' password '])) {//If the session is empty and the user does not choose to record the logon form
Header ("location:login.php?req_url=". $_server[' Request_uri ')); Go to the login page, record the requested URL, login and jump past, the user experience is good.
}else{//user has chosen to remember login status
$user = GetUserInfo ($_cookie[' username '],$_cookie[' password ')); To access the user's profile
if (empty ($user)) {//username password not to access information, go to the 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 Code code 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 = "Both user name and password cannot be null";
}else{
$row = GetUserInfo ($username, $password);

if (empty ($row)) {
$err _msg = "User name and password are incorrect";
}else{
$_session[' user_info ' = $row;
if (!empty ($remember)) {//If the user chooses, record the login status and place the username and encryption password in 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 point exits, clear the record login status
Copy Code code 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

Copy Code code as follows:

?
Read the user name and password value of the cookie
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= "<?= $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>
?
Log in, save username and password to cookie
if ($_post[' button ']!= ') {
$uname = $_post[' uname '];
$pwd = $_post[' pwd '];
If the encrypted password entered is not the same as 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);
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.