Php cookie login verification code

Source: Internet
Author: User
Tags reset setcookie
The code is as follows: Copy code

<Html>
<Head>
<Title> Login </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<Form name = "form1" method = "post" action = "login. php">
<Table width = "300" border = "0" align = "center" cellpadding = "2" cellspacing = "2">
<Tr>
<Td width = "150"> <div align = "right"> user name: </div> </td>
<Td width = "150"> <input type = "text" name = "username"> </td>
</Tr>
<Tr>
<Td> <div align = "right"> Password: </div> </td>
<Td> <input type = "password" name = "passcode"> </td>
</Tr>
<Tr>
<Td> <div align = "right"> Cookie retention Time: </div> </td>
<Td> <select name = "cookie" id = "cookie">
<Option value = "0" selected> browser process </option>
<Option value = "1"> save for 1 day </option>
<Option value = "2"> save for 30 days </option>
<Option value = "3"> 365 days </option>
</Select> </td>
</Tr>
</Table>
<P align = "center">
<Input type = "submit" name = "Submit" value = "Submit">
<Input type = "reset" name = "Reset" value = "Reset">
</P>
</Form>
</Body>
</Html>

Bytes -------------------------------------------------------------------------------------------------------------------------

The code is as follows: Copy code

<? Php
@ Mysql_connect ("localhost", "root", "1981427") // you must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("test") // select the database mydb
Or die ("The database does not exist or is unavailable ");
// Obtain user input
$ Username = $ _ POST ['username'];
$ Passcode = $ _ POST ['passcode'];
$ Cookie = $ _ POST ['cooker'];
// Execute the SQL statement
$ Query = @ mysql_query ("select username, userflag from users"
. "Where username = '$ username' and passcode =' $ passcode '")
Or die ("SQL statement execution failed ");
// Determine whether the user exists and the password is correct
If ($ row = mysql_fetch_array ($ query ))
{
If ($ row ['userflag'] = 1 or $ row ['userflag'] = 0) // Determine whether the user permission information is valid
{
Switch ($ cookie) // Set the cookie retention time based on the user's selection
   {
Case 0: // Save the Cookie as a browser process
Setcookie ("username", $ row ['username']);
Break;
Case 1: // save for 1 day
Setcookie ("username", $ row ['username'], time () + 24*60*60 );
Break;
Case 2: // save for 30 days
Setcookie ("username", $ row ['username'], time () + 30*24*60*60 );
Break;
Case 3: // save 365 days
Setcookie ("username", $ row ['username'], time () + 365x24*60*60 );
Break;
   }
Header ("location: main. php"); // automatically jumps to main. php
}
Else
{
Echo "incorrect user permission information ";
}
}
Else
{
Echo "incorrect user name or password ";
}
?>

Bytes -------------------------------------------------------------------------------------------------------------------------

The code is as follows: Copy code

<? Php
Session_start ();
If (isset ($ _ COOKIE ['username'])
{
@ Mysql_connect ("localhost", "root", "1981427") // you must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("test") // select the database mydb
Or die ("The database does not exist or is unavailable ");
// Obtain the Session
$ Username = $ _ COOKIE ['username'];
// Execute the SQL statement to obtain the userflag value
$ Query = @ mysql_query ("select userflag from users"
. "Where username = '$ username '")
Or die ("SQL statement execution failed ");
$ Row = mysql_fetch_array ($ query );
// Obtain user permission information
$ Flag = $ row ['userflag'];
// Output different welcome information based on the userflag value
If ($ flag = 1)
Echo "welcome administrator". $ _ SESSION ['username']. "log on to the system ";
If ($ flag = 0)
Echo "welcome". $ _ SESSION ['username']. "log on to the system ";
Echo "<a href =" logout. php "mce_href =" logout. php "> logout </a> ";
}
Else
{
Echo "you are not authorized to access this page ";
}
?>

Bytes -------------------------------------------------------------------------------------------------------------------------

The code is as follows: Copy code

<? Php
Setcookie ("username ");
Echo "logout successful ";
?>

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.