PHP Cookie Login Verification Code _php Tutorial

Source: Internet
Author: User
Tags set cookie
Provide an automatic cookie login method, the user can choose to save the process during login days and months, we just came in the page to judge the KO.
The code is as follows Copy Code



Login





--------------------------------------------------------------------------------------------------------------- ----------

The code is as follows Copy Code

@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
Get user input
$username = $_post[' username ');
$passcode = $_post[' passcode ');
$cookie = $_post[' Cookie '];
Execute SQL statement
$query = @mysql_query ("Select Username, userflag from users"
." where username = ' $username ' and passcode = ' $passcode ' ")
Or Die ("SQL statement execution failed");
Determine if the user exists, the password is correct
if ($row = mysql_fetch_array ($query))
{
if ($row [' userflag '] = = 1 or $row [' userflag '] = = 0)//Determine if user rights information is valid
{
Switch ($cookie)//Set cookie save time according to user's choice
{
Case 0://Save cookie for browser process
Setcookie ("username", $row [' username ']);
Break
Case 1://Save 1 days
Setcookie ("username", $row [' username '], time () +24*60*60);
Break
Case 2://Save 30 days
Setcookie ("username", $row [' username '], time () +30*24*60*60);
Break
Case 3://Save 365 Days
Setcookie ("username", $row [' username '], time () +365*24*60*60);
Break
}
Header ("location:main.php"); Auto Jump to main.php
}
Else
{
echo "User rights information is incorrect";
}
}
Else
{
echo "User name or password error";
}
?>

--------------------------------------------------------------------------------------------------------------- ----------

The code is as follows Copy Code
< p>!--? php
session_start ();
if (isset ($_cookie[' username '))
{
@mysql_connect ("localhost", "root", "1981427")//database server needs to be connected before selecting a database
or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select database MyDB
or Die ("database does not exist or is unavailable");
Get session
$username = $_cookie[' username ');
The Execute SQL statement obtains the value of Userflag
$query = @mysql_query ("Select Userflag from Users"
. " where username = ' $username ')
or Die ("SQL statement execution failed");
$row = Mysql_fetch_array ($query);
//Get user rights information
$flag = $row [' Userflag '];
Output different welcome information according to the value of Userflag
if ($flag = = 1)
echo "Welcome administrator". $_session[' username ']. " Login System ";
if ($flag = = 0)
echo "Welcome user". $_session[' username ']. " Login System ";
echo "Logout";
}
Else
{
echo "You do not have permission to access this page";
}
?

--------------------------------------------------------------------------------------------------------------- ----------

The code is as follows Copy Code

Setcookie ("username");
echo "Logout success";
?>

http://www.bkjia.com/PHPjc/631700.html www.bkjia.com true http://www.bkjia.com/PHPjc/631700.html techarticle provide an automatic cookie login method, the user can choose to save the process during login days and months, we just came in the page to judge the KO. Code to copy code as follows ...

  • 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.