PHP Cookie-enabled login verification effect Cookie
The code is as follows:
Login
The code is as follows:
@ 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 ";
}
?>
The code is as follows:
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 "logout ";
}
Else
{
Echo "you are not authorized to access this page ";
}
?>
The code is as follows:
Setcookie ("username ");
Echo "logout successful ";
?>