Use cookies to save page login information
1. Database Connection Configuration page: connectvars.php
Copy the Code code as follows:
Location of the database
Define (' db_host ', ' localhost ');
User name
Define (' Db_user ', ' root ');
Password
Define (' Db_password ', ' 19900101 ');
Database name
Define (' db_name ', ' test ');
?>
2. Login page: login.php
Copy the Code code as follows:
Inserting information about a connected database
Require_once ' connectvars.php ';
$error _msg = "";
Determine if the user has set a COOKIE, and if $_cookie[' user_id ' is not set, execute the following code
if (!isset ($_cookie[' user_id ')) {
if (Isset ($_post[' submit ')) {//Determine if the user submits the login form, if yes, execute the following code
$DBC = Mysqli_connect (db_host,db_user,db_password,db_name);
$user _username = mysqli_real_escape_string ($dbc, trim ($_post[' username '));
$user _password = mysqli_real_escape_string ($dbc, trim ($_post[' password '));
if (!empty ($user _username) &&!empty ($user _password)) {
The SHA () function in MySQL is used for one-way encryption of strings
$query = "Select user_id, username from mismatch_user WHERE username = ' $user _username ' and '." Password = SHA (' $user _password ') ";
Querying with a user name and password
$data = Mysqli_query ($dbc, $query);
If the record is exactly one, set the cookie and redirect the page
if (Mysqli_num_rows ($data) ==1) {
$row = Mysqli_fetch_array ($data);
Setcookie (' user_id ', $row [' user_id ']);
Setcookie (' username ', $row [' username ']);
$home _url = ' loged.php ';
Header (' Location: '. $home _url);
}else{//If the record is incorrect, set the error message
$error _msg = ' Sorry, you must enter a valid username and password to log in. ';
}
}else{
$error _msg = ' Sorry, you must enter a valid username and password to log in. ';
}
}
}else{//If the user is already logged in, jump directly to the already logged in page
$home _url = ' loged.php ';
Header (' Location: '. $home _url);
}
?>
<title>Mismatch-log in</title>
Msimatch-log in
if (Empty ($_cookie[' user_id ')) {
Echo '
'. $error _msg. '
';
?>
}
?>
:
3. Login page: loged.php
Copy the Code code as follows:
Logged in page, display login user name
if (isset ($_cookie[' username ')) {
Echo ' You are logged as '. $_cookie[' username '. '
';
Click "Log Out" to go to the logout.php page to log off the cookie
Echo ' Log out ('. $_cookie[' username ') ';
}
/** on the login page, you can take advantage of the user's COOKIE such as $_cookie[' username '),
* $_cookie[' user_id '] query the database, you can do a lot of things * *
?>
:
4. Logout Cookie page: logout.php (redirected to lonin.php after logout)
Copy the Code code as follows:
/**cookies Logout Page */
if (Isset ($_cookie[' user_id ')) {
Set the expiry time of each cookie to a time in the past, so that it is deleted by the system, in seconds
Setcookie (' user_id ', ', Time ()-3600);
Setcookie (' username ', ', Time ()-3600);
}
Location header redirects the browser to another page
$home _url = ' login.php ';
Header (' Location: '. $home _url);
?>
The above describes the tracking cookie PHP in the use of cookies to save user login information implementation code, including the content of tracking cookies, I hope that the PHP tutorial interested in a friend helpful.