Code in PHP to remember the automatic login of the password

Source: Internet
Author: User
Tags exit copy empty header log md5 setcookie trim
PHP implementation Remember password automatic login method more than one, there are two emptyempty, in fact, it is because the code highlighted a bug. Hope to be helpful to everyone.

A, user log in check
Copy CodeThe code is as follows:
Check if the user is logged on
function Checklogin () {
if (Emptyempty ($_session[' User_info ')) {//check if the session is empty
if (Emptyempty ($_cookie[' username ')) emptyempty ($_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, login and jump past, the user experience is good.
}else{//user has chosen to remember login status
$user = GetUserInfo ($_cookie[' username '],$_cookie[' password ')); To access the user's profile
if (Emptyempty ($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 personal data into the session
}
}
}
}
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, login and jump past, the user experience is good.
}else{//user has chosen to remember login status
$user = GetUserInfo ($_cookie[' username '],$_cookie[' password ')); To access the 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 personal data into the session
}
}
}
}

When you visit every page in the background, check it first.

Two, the user submits the login information
When the user fills in the username and password and submits it here,
Copy CodeThe code is as follows:
$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 verification code";
}elseif ($username = = "$password = =") {
$err _msg = "Both user name and password cannot be null";
}else{
$row = GetUserInfo ($username, $password);

if (Emptyempty ($row)) {
$err _msg = "User name and password are incorrect";
}else{
$_session[' user_info ' = $row;
if (!emptyempty ($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");
}
}
}
$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 verification code";
}elseif ($username = = "$password = =") {
$err _msg = "Both user name and password cannot be null";
}else{
$row = GetUserInfo ($username, $password);

if (empty ($row)) {
$err _msg = "User name 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");
}
}
}

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.
Copy CodeThe code is as follows:
Exit Login
function logout () {
unset ($_session[' user_info '));
if (!emptyempty ($_cookie[' username ')) emptyempty ($_cookie[' password ')) {
Setcookie ("username", NULL, Time () -3600*24*365);
Setcookie ("password", NULL, Time () -3600*24*365);
}
}


Related Article

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.