In php, session is used to save user login information. Session is a very important thing in php. session is usually used for user login. session is much safer than cookie, at the same time, our property car session is a very important thing in php. for example, session is usually used for user login. session is much safer than cookie, at the same time, our car often uses session to save temporary records.
Simple session creation
The code is as follows: |
|
Session_start (); $ Username = "nostop "; Session_register ("username "); ?> |
In this example, we registered a variable named username with the value of nostop to the session.
Read session
PHP's built-in $ _ SESSION variable allows you to easily access the set session variable.
The code is as follows: |
|
Example: Session_start (); Echo "the registered user name is:". $ _ SESSION ["username"]; // The output user name is nostop. ?> |
Destroy session
The code is as follows: |
|
Session_unregister () cancels a single session variable Unset ($ _ SESSION ['age']); used to cancel a SESSION variable registered with $ _ session ['age '] Session_unset () delete all registered variables Session_destroy (): cancels all session variables and cancels the entire session |
Example:
The code is as follows: |
|
Session_start (); Session_unregister ("username"); // deregister a session variable Session_unset (); // deregister a session ?> |
Let's look at a complete session usage method,
Use session to save user login information
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 '); ?>
|
Logon page: logIn. php
The code is as follows: |
|
// 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 ')"; // 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 ['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 (! Isset ($ _ SESSION ['User _ id']) { Echo' '. $ Error_msg .' '; ?> } ?>
|
3. logon page: loged. php
The code is as follows: |
|
// You must enable the session before using the variable values stored in the session. Session_start (); // Use a session variable to check the logon status If (isset ($ _ SESSION ['username']) { Echo 'you are Logged as '. $ _ SESSION ['username'].' '; // Click "Log Out" and go to the logOut page to Log Out. Echo 'log Out ('. $ _ SESSION ['username'].') '; } /** On the logon page, you can use the user's session, for example, $ _ SESSION ['username'], * $ _ SESSION ['User _ id'] queries the database, so you can do a lot of things */ ?>
|
4. logOut session Page: logOut. php (redirection to lonIn. php after logOut)
The code is as follows: |
|
// 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 the SESSION variable, set the $ _ 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 (); } // The location header redirects the browser to another page $ Home_url = 'login. php '; Header ('Location: '. $ home_url ); ?> |
Vivo is much safer. at the same time, our belongings and cars are often used...