Php uses cookies to remember the logon status and phpcookie logon status

Source: Internet
Author: User

Php uses cookies to remember the logon status and phpcookie logon status

To implement the function of remembering automatic Password Logon, most of our data is implemented using cookies on the client. We use php, which is no exception. If you need it, please refer to it.

Php creates a solution for remembering the password for automatic login, that is, to operate sessions and cookies.

1. Check whether the user is logged on
Copy codeThe Code is as follows:
// Check whether the user logs on
Function checklogin (){
If (empty ($ _ SESSION ['user _ info']) {// check whether the session is empty.
If (empty ($ _ COOKIE ['username']) | empty ($ _ COOKIE ['Password']) {// if the session is empty, the user does not select to log on.
Header ("location: login. php? Req_url = ". $ _ SERVER ['request _ URI ']); // go to the logon page and record the REQUEST url. After logging on, go to the logon page. The user experience is good.
} Else {// remember the logon status
$ User = getUserInfo ($ _ COOKIE ['username'], $ _ COOKIE ['Password']); // retrieves the user's personal data
If (empty ($ user) {// if the user name and password are incorrect, the information is not obtained. Go to the logon page.
Header ("location: login. php? Req_url = ". $ _ SERVER ['request _ URI ']);
} Else {
$ _ SESSION ['user _ info'] = $ user; // the user name and password are correct. Put the user's personal data in the session.
}
}
}
}

2. the user submits logon 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 = "neither user name nor password can be blank ";
} Else {
$ Row = getUserInfo ($ username, $ password );

If (empty ($ row )){
$ Err_msg = "the user name and password are incorrect ";
} Else {
$ _ SESSION ['user _ info'] = $ row;
If (! Empty ($ remember) {// if the user chooses to log on, the user name and password added are recorded 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 ");
}
}
}

3. When the user points out, the logon status is cleared.
Copy codeThe Code is as follows:
// Log 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 );
}
}

4. Simplified Version instances
Copy codeThe Code is as follows:
<?
// Read the COOKIE username and password value.
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 on and save the user name and password to the COOKIE
If ($ _ POST ['click']! = ''){
$ Uname = $ _ POST ['uname'];
$ Pwd = $ _ POST ['pwd'];
// If the entered encryption password is different from that in the COOKIE, it will be encrypted
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.