PHP implements automatic login by remembering the password

Source: Internet
Author: User

(This article reproduced: http://hi.baidu.com/pcbbt/item/03a53f3cda9827ffde222149)

 

PHP implements automatic login by remembering the password

PHP implements more than one method to remember the password for automatic login.

1. User Logon check

// 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, and the user does not choose to record the login header ("Location: login. PHP? Req_url = ". $ _ server ['request _ URI ']); // go to the login page, record the request URL, and jump to the past after logon. the user experience is good. } Else {// The user selects the remember login Status $ user = getuserinfo ($ _ cookie ['username'], $ _ cookie ['Password']); // retrieve the user's personal data if (empty ($ user) {// if the user name and password are incorrect, go to the login 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 into the session }}}}

 

Perform the preceding check before accessing every page in the background.

2. the user submits logon information
Submit the user name and password here,
Copy the 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 = "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 selects this option, the user name and password added are recorded in the logon status and placed 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 ");}}}

 

For a brief explanation of $ ref_url, assume that user a accesses user B. PHP, but user A does not log on, jump to the login page login. PHP. after entering the user and password on the logon page, confirm and redirect to B. PHP page, instead of redirecting to a default page main_user.php. Because B. php is the page that user a wants to visit, the user experience will be better.

3. When the user points out, the logon status is cleared.

Why do you want to do this? If someone else uses your computer, they may browse your personal privacy. So when the user deliberately quits, it is best to cancel the log logon status.
// 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);   } }

 

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.