This article provides a detailed analysis of the implementation code that saves user login information by using session and cookie in php. For more information, see
This article provides a detailed analysis of the implementation code that saves user login information by using session and cookie in php. For more information, see
Both session and cookie are used to save user logon information.
1. Database Connection configuration page: connectvars. php
The Code is as follows:
// Database location
Define ('db _ host', '2017. 0.0.1 ');
// 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 ';
// Start a session
Session_start ();
$ Error_msg = "";
// If the user is not logged on, that is, when $ _ SESSION ['user _ id'] is not set, run the following code:
If (! Isset ($ _ SESSION ['user _ id']) {
If (isset ($ _ POST ['submit ']) {// execute the following code when the user submits the logon form
$ 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 ')";
$ Data = mysqli_query ($ dbc, $ query );
// Query with the user name and password. If the record is exactly one, set the SESSION and COOKIE, and redirect the page at the same time.
If (mysqli_num_rows ($ data) = 1 ){
$ Row = mysqli_fetch_array ($ data );
$ _ SESSION ['user _ id'] = $ row ['user _ id'];
$ _ SESSION ['username'] = $ row ['username'];
Setcookie ('user _ id', $ row ['user _ id'], time () + (60*60*24*30 ));
Setcookie ('username', $ row ['username'], time () + (60*60*24*30 ));
$ 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 to the VM in Hong Kong, the logon page is displayed.
$ Home_url = 'loged. php ';
Header ('location: '. $ home_url );
}
?>
Mismatch-Log In
Msimatch-Log In
If (! Isset ($ _ SESSION ['user _ id']) {
Echo'
'. $ Error_msg .'
';
?>
}
?>
:
4. logOut session and cookie page: logOut. php (redirection to lonIn. php after logOut)
The Code is as follows:
/** Cancel the session and cookie simultaneously */
// Even if you log out, you must start the session before accessing the session variable.
Session_start ();
// Use a session variable to check the logon status
If (isset ($ _ SESSION ['user _ id']) {
// To clear SESSION variables, Vm, set $ _ SESSION super global variable to an empty array
$ _ SESSION = array ();
// If a session cookie exists, delete it by setting the expiration time to the previous one hour
If (isset ($ _ COOKIE [session_name ()]) {
Setcookie (session_name (), '', time ()-3600 );
}
// Use the built-in session_destroy () function to call and cancel a session
Session_destroy ();
}
// At the same time, set the expiration time of each cookie to a certain time in the past so that they can be deleted by the system. The Hong Kong server is rented and the time is 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 );
?>