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.