Php uses $ _ COOKIE to implement automatic login program code

Source: Internet
Author: User
The principle of automatic logon is that the user saves the user name and password in cookies during the first logon, so that the user name and password in the cookies value are determined at the next logon to be the same as those in the database, if yes, log on automatically and set the session... the principle of automatic logon is that the user saves the user name and password in cookies during the first logon, so that the user name and password in the cookies value are determined at the next logon to be the same as those in the database, if yes, you can automatically log on and set the session as follows.

When creating a website, you will often encounter the need to remember the password. The next time you log on automatically, you will not log on within one week, and you will not log on within one month. This function is generally implemented through cookies. This article will briefly explain how to use php to achieve this requirement. Of course, there are more than N methods to achieve this requirement.

The whole process is when you log on, if you choose to remember the password or do not log on within one week, after the user successfully logs on, store the data of a cookie for automatic logon to the user table of the database for verification at next automatic logon. If the verification succeeds, the system automatically logs on. Otherwise, you need to enter the user name and password to log on. The cookie value can be a random code.

The sample code is as follows:

 

In addition, you must perform the following function checks before accessing each page of the website.

// 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 logon page and record the REQUEST url. after logging on, go to the logon page. 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, the information is not obtained. 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 }}}}


Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.