Session in PHP is a very important thing, like our user login is generally used to the session of this thing, relative to the cookie session to a lot of security, and our shopping carts often use the session to do temporary records save OH.
Save page Login information using session
1. Database Connection Configuration page: connectvars.php
<?php
//Database location
define (' db_host ', ' localhost ');
Username
define (' Db_user ', ' root ');
Password
define (' Db_password ', ' 19900101 ');
Database name
define (' db_name ', ' test ');
? >
2, login page: login.php
<?php//Insert the relevant information of the connection database require_once ' connectvars.php ';
Open a session session_start ();
$error _msg = ""; If the user is not logged in, that is, if $_session[' user_id ' is not set, execute 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 ')); The SHA () function in the IF (!empty ($user _username) &&!empty ($user _password)) {//mysql is used for one-way encryption of strings $query = "Select User _id, username from mismatch_user WHERE username = ' $user _username ' and '.
Password = SHA (' $user _password ') ";
Query with username and password $data = mysqli_query ($dbc, $query);
If the record is exactly one, then set the session, while the page redirection 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 is not correct, set the error message $error _msg = ' Sorry, you must enter a valid username and password to log in. '
}else{$error _msg = ' Sorry, you are must enter a valid username and password to log in. '
}}else{//If the user is already logged in, jump directly to the already logged on page $home _url = ' loged.php ';
Header (' Location: '. $home _url); }?>
3, login page: loged.php
the session
Session_Start () must be opened before <?php//using variable values stored in the session;
Use a session variable to check the login status
if (isset ($_session[' username ')) {
echo ' you are logged as '. $_session[' username ']. ' <br/> ';
Click "Log Out" to go to the LogOut page to unregister the
echo ' <a href= ' logout.php ' > Log Out ('. $_session[' username ']. ') </a> ';
}
/** in the login page, you can use the user's session such as $_session[' username ',
* $_session[' user_id '] to query the database, you can do a lot of things * * *
?>
4, Logout session page: logout.php (after logout redirect to lonin.php)
<?php
//Even when logging off, you must start the session first to access the session variable
session_start ();
Use a session variable to check the login 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 is present, delete it if
(Isset ()) {Setcookie () by setting the expiration time to the previous 1 hours $_cookie[session_name ()
, ", Time () -3600);
}
Use the built-in Session_destroy () function to invoke the undo session
Session_destroy ();
}
The location header redirects the browser to another page
$home _url = ' login.php ';
Header (' Location: '. $home _url);
? >
Okay, the above code is the entire content of this article, the code is very simple, many places are accompanied by annotation text, where there is no understanding of the place welcome to my message, I will give you the first time to answer. Thank you!