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 );
?> |