PHP Remember password automatic login

Source: Internet
Author: User
Tags setcookie
When we log in to the site management backstage, there will be prompted to remember the status of login, remember me and so on, this option is what to use it. If you check the log in state, the next time you browse the Web site does not need to fill in the user name and password, in the log in. Here is a small code, a simple description of the idea of automatic login. There are more than one way to say what I think.

A, user log in check
Check whether 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 has not selected to record the login
            header (' location:login.php?req_url= '. $_server[' Request_uri ']);
        {
            //the user has chosen to remember the login status
            $user = GetUserInfo ($_cookie[' username '], $_cookie[' password ']);
            To fetch the user's profile
            if (empty ($user)) {
                //username password not to get information, go to the login page
                header (' location:login.php?req_url= '. $_ server[' Request_uri ']);
            else {
                $_session[' user_info ' = $user;}}}}
When you visit every page in the background, check it first.

Second, the user submits the login information
When the user fills in the username and password, submit it here.
 $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 authentication code ';} elseif ($username = = ' | | $password = = ') {
$err _msg = ' username 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 password in the cookie Setcookie (' username ', $username, t
            IME () + 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 '); }
    }
}
About $ref_url Simple explanation, if: User A to visit b.php, but a user does not login, jumps to the login page login.php, after completing the login page the user and the password, determines then jumps to b.php this page, instead of jumps a default page main_ user.php. Because b.php is the page that user A wants to go to, the user experience will be better.

Third, when the user point exits, clear the record login status
Why do this, because if someone else uses your computer, they may be able to browse to your privacy, so when the user deliberately click to exit, it is best to record the status of the log off.
Exits login
function logout ()
{
    unset ($_session[' user_info '));
    if (!empty ($_cookie[' username ')) | |!empty ($_cookie[' password '])) {
        Setcookie (' username ', NULL, Time ()-3600 * 24 * 3 );
        Setcookie (' password ', NULL, Time ()-3600 * 365);
    }

Reprint Please specify
Author: Undersea Eagle
Address: http://blog.51yip.com/php/1001.html



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.