In php, the following example shows how to remember the password for next automatic logon. _ PHP Tutorial

Source: Internet
Author: User
Tags php example
In php, the example of remembering the next automatic login password is remembered. In php, the following example shows how to remember the password for the next automatic login. remember that the password is often used to log on to a website. remember the password for the next automatic login, and do not log on within one week, in a php example, remember the password for the next automatic login, remember the password for automatic login

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:

The code is as follows:


$ Username = trim ($ _ POST ['username']);
$ Password = md5 (trim ($ _ POST ['password']);
$ Ref_url = $ _ GET ['req _ url'];
$ Remember = $ _ POST ['member']; // indicates whether to log on automatically.
$ Err_msg = '';
If ($ 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 ");
}
}
}

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

The 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.
}
}
}
}


Php will log on automatically next time

It is generally determined by cookie to determine whether a user logs on or not. Therefore, this implementation usually relies on setting the cookie time to determine whether the user remains logged on, if you keep the cookie, set the time to a longer value. It can be set for a specific period of time, one day, one month, or one year.
Setcookie ("TestCookie", $ value, time () + 3600 );
Here, time () + 3600 is the time, which means that the logon status is maintained for an hour from now on.

How to remember the user name and password in php

SESSION and COOKIE are designed to solve this problem. you must keep using cookies on the local machine.
 

When a hacker is doing a website, he will often encounter the need to remember the password. The next automatic login will be avoided within one week, one...

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.