PHP implementation remember the next automatic login example _php instance

Source: Internet
Author: User

Do the site often encounter to realize the password, the next automatic login, a week without landing, one months to avoid this demand. This functionality is generally done through cookies. This article will briefly describe how to use PHP to implement this requirement. Of course there are many n ways to implement this requirement.

The whole process is the user in the landing, if you choose to remember the password or a week without landing, such as this option, then after the user successfully logged the operation completed, store a implementation of the automatic login cookie data to the database user table, as the next automatic login verification. Verify that you are logged in automatically, or you will need to enter a username and password to log in. The saved cookie value can take a random code.

The specific example code is as follows:

Copy Code code as follows:

$username =trim ($_post[' username '));
$password =md5 (Trim ($_post[' password '));
$ref _url=$_get[' Req_url '];
$remember =$_post[' Remember '];//automatically sign in
$err _msg= ';
if ($username = = "' | | $password = = ") {
$err _msg= "User name and password cannot be null";
}else{
$row =getuserinfo ($username, $password);
if (empty ($row)) {
$err _msg= "username and password are incorrect";
}else{
$_session[' User_info ']= $row;
if (!empty ($remember)) {//If the user chooses, record the login status and place the username and encryption password 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, when you visit each page of the site, you must first check the following function.

Copy Code code as follows:

Check if the user is logged on
function Checklogin () {
if (Empty ($_session[' user_info ')) {//check if 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 logon form
Header ("location:login.php?req_url=". $_server[' Request_uri ');/go to the login page, record the requested URL, log in and jump past, the user experience good.
}else{//user has chosen to remember login status
$user =getuserinfo ($_cookie[' username '],$_cookie[' password '));//access to user's profile
if (empty ($user)) {//username password not to access information, go to the login page
Header ("location:login.php?req_url=". $_server[' Request_uri '));
}else{
$_session[' User_info ']= $user;//username and password right, put the user's profile in the session
}
}
}
}

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.