Php uses cookies to save the user login information implementation code. if you are using php, you can use cookies to save the login information on the page.
1. database connection configuration page: connectvars. php
The code is as follows:
// Database location
Define ('Db _ host', 'localhost ');
// User name
Define ('Db _ user', 'root ');
// Password
Define ('Db _ password', '123 ');
// Database name
Define ('Db _ name', 'test ');
?>
2. logon page: logIn. php
The code is as follows:
// Insert information related to the database connection
Require_once 'connectvars. php ';
$ Error_msg = "";
// Determine whether the user has set a cookie. if $ _ COOKIE ['User _ id'] is not set, run the following code:
If (! Isset ($ _ COOKIE ['User _ id']) {
If (isset ($ _ POST ['submit ']) {// determines whether the user has submitted the logon form. if yes, run 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 to encrypt strings in one way.
$ Query = "SELECT user_id, username FROM mismatch_user WHERE username = '$ user_username' AND". "password = SHA ('$ user_password ')";
// Query by user name and password
$ Data = mysqli_query ($ dbc, $ query );
// If one record is found, 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 found is incorrect, the error message is set.
$ 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 has logged on, go directly to the logged on page
$ Home_url = 'loged. php ';
Header ('Location: '. $ home_url );
}
?>
Mismatch-Log In
Msimatch-Log In
If (empty ($ _ COOKIE ['User _ id']) {
Echo'
'. $ Error_msg .'
';
?>
}
?>
:
3. logon page: loged. php
The code is as follows:
// The logon user name is displayed on the logon page.
If (isset ($ _ COOKIE ['username']) {
Echo 'you are Logged as '. $ _ COOKIE ['username'].'
';
// Click "Log Out" and go to the logOut. php page to Log Out the cookie.
Echo 'log Out ('. $ _ COOKIE ['username'].') ';
}
/** On the logon page, you can use your cookies, such as $ _ cookie ['username'],
* $ _ COOKIE ['User _ id'] queries the database, so you can do a lot of things */
?>
:
4. log out cookie page: logOut. php (redirection to lonIn. php after logging out)
The code is as follows:
/** Cancel cookies page */
If (isset ($ _ COOKIE ['User _ id']) {
// Set the expiration time of each cookie to a certain time in the past so that they can be deleted by the system, in seconds
Setcookie ('User _ id', '', time ()-3600 );
Setcookie ('username', '', time ()-3600 );
}
// The location header redirects the browser to another page
$ Home_url = 'login. php ';
Header ('Location: '. $ home_url );
?>