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 car often uses session to save temporary records.
Simple session Creation
| The Code is as follows: |
Copy code |
<? 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: |
Copy code |
Example: <? Php Session_start (); Echo "the registered user name is:". $ _ SESSION ["username"]; // The output user name is nostop. ?> |
Destroy session
| The Code is as follows: |
Copy code |
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: |
Copy code |
<? Php 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: |
Copy code |
<? Php // 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: |
Copy code |
// 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 ); } ?> <Html> <Head> <Title> Mismatch-Log In </title> <Link rel = "stylesheet" type = "text/css" href = "style.css"/> </Head> <Body> <H3> Msimatch-Log In <! -- Judge by $ _ SESSION ['user _ id']. If the user is not logged on, the logon form is displayed, asking the user to enter the user name and password --> <? Php If (! Isset ($ _ SESSION ['user _ id']) { Echo '<p class = "error">'. $ error_msg. '</p> '; ?> <! -- $ _ SERVER ['php _ SELF '] indicates that when a user submits a form, the user calls his/her own PHP file. --> <Form method = "post" action = "<? Php echo $ _ SERVER ['php _ SELF '];?> "> <Fieldset> <Legend> Log In </legend> <Label for = "username"> Username: </label> <! -- If the user name has been lost, the user name is displayed. --> <Input type = "text" id = "username" name = "username" Value = "<? Php if (! Empty ($ user_username) echo $ user_username;?> "/> <Br/> <Label for = "password"> Password: </label> <Input type = "password" id = "password" name = "password"/> </Fieldset> <Input type = "submit" value = "Log In" name = "submit"/> </Form> <? Php } ?> </Body> </Html> |
3. logon page: loged. php
| The Code is as follows: |
Copy code |
<? Php // 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'].' <br/> '; // Click "Log Out" and go to the logOut page to Log Out. Echo '<a href = "logOut. php"> Log Out ('. $ _ SESSION ['username']. ') </a> '; } /** 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: |
Copy code |
<? Php // 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 ); ?> |