: This article describes how to use session in php to save user logon information. For more information about PHP tutorials, see. Php uses session to save user login information
Use session to save logon information on the page
1. database connection configuration page: connectvars. php
2. logon page: logIn. php
Connect to databaseRelated informationRequire_ Once 'varvars. php '; // when a SESSION session_start (); $ error_msg = ""; // if the user is not logged on, $ _ SESSION ['User _ id'] is not set, run the following code: if (! Isset ($ _ SESSION ['User _ id']) {if (isset ($ _ POST ['submit ']) {// when the user submits the login form, 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 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 SESSION and redirect the page if (mysqli_num_rows ($ data) = 1) {$ row = mysqli_fetch_array ($ data ); $ _ SESSION ['User _ id'] = $ row ['User _ id']; $ _ SESSION ['username'] = $ row ['use Rname']; $ 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 has logged on, go directly to the logged on page $ home_url = 'loged. php '; header ('Location :'. $ home_url) ;}?> Mismatch-Log In
Msimatch-Log In
'. $ Error_msg .'';?>
3. logon page: loged. php
'; // Click "Log Out" and go to the logOut page to cancel echo 'log Out ('. $ _ SESSION ['username']. ')';}/** on the logged-on page, you can use a user's session such as $ _ SESSION ['username'] and * $ _ SESSION ['User _ id'] to query the database. how many things can be done? */?>
4. logOut session Page: logOut. php (redirection to lonIn. php after logOut)
Global variablesSet to an empty array $ _ SESSION = array (); // if a SESSION cookie exists, you can set the expiration time to the previous one hour to delete it if (isset ($ _ COOKIE [session_name ()]) {setcookie (session_name (),'', time ()-3600);} // use the built-in session_destroy () function to call the undo session session_destroy ();} // The location header redirects the browser to another page $ home_url = 'login. php '; header ('Location :'. $ home_url);?>
The above describes how to use session in php to save user login information, including require, global variables, and connection to the database. I hope to help anyone who is interested in PHP tutorials.