Session in PHP to save user login Information _php tutorial

Source: Internet
Author: User
Session in PHP is a very important thing, like our user login is generally used to the session of this thing, compared to the cookie to the session to be a lot safer, and our property vehicles often use the session to do temporary record save Oh.

Simple to create session

The code is as follows Copy Code

Session_Start ();
$username = "Nostop";
Session_register ("username");
?>

In this example, we register a variable named username to the session with a value of nostop.

Read session

PHP built-in $_session variables can be easily accessed by setting the SESSION variable.

The code is as follows Copy Code

Example:
Session_Start ();
echo "Registered user name:". $_session["username"]; The output registered user name is: nostop
?>

Destroy session

The code is as follows Copy Code

Session_unregister () unregisters a single session variable
unset ($_session[' age '); Used to unregister SESSION variables registered with $_session[' age ')
Session_unset () Delete all registered variables
Session_destroy () Unregisters all session variables, and logs off entire sessions

Example:

The code is as follows Copy Code
Session_Start ();
Session_unregister ("username"); Log off a Session variable
Session_unset (); Unregistering Session Sessions
?>


Look at a complete session using the method,

Use session to save user login information

The code is as follows Copy Code


Location of the database
Define (' db_host ', ' localhost ');
User name
Define (' Db_user ', ' root ');
Password
Define (' Db_password ', ' 19900101 ');
Database name
Define (' db_name ', ' test ');
?>

Login page: login.php

The code is as follows Copy Code

If the user is not logged in, that is, $_session[' user_id ' is not set, execute the following code
if (!isset ($_session[' user_id ')) {
if (Isset ($_post[' submit ')) {//The following code is executed when the user submits the login 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 for one-way encryption of strings
$query = "Select user_id, username from mismatch_user WHERE username = ' $user _username ' and '." Password = SHA (' $user _password ') ";
Querying with a user name and password
$data = Mysqli_query ($dbc, $query);
If the record is exactly one, 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 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 is already logged in, jump directly to the already logged in page
$home _url = ' loged.php ';
Header (' Location: '. $home _url);
}
?>


<title>Mismatch-log in</title>



Msimatch-log in



if (!isset ($_session[' user_id ')) {
Echo '

'. $error _msg. '

';
?>


}
?>


3. Login page: loged.php

The code is as follows Copy Code


Session must be started before using variable values stored in the session
Session_Start ();
Check login status with a session variable
if (isset ($_session[' username ')) {
Echo ' You are logged as '. $_session[' username '. '
';
Click "Log Out" to go to the logout page to sign out
Echo ' Log out ('. $_session[' username ') ';
}
/** in the login page, you can take advantage of the user's SESSION such as $_session[' username '],
* $_session[' user_id '] query the database, you can do a lot of things * *
?>

4. Logout Session page: logout.php (redirected to lonin.php after logout)

The code is as follows Copy Code

Even when you log off, you must start the session first to access the session variable
Session_Start ();
Check login status with a session variable
if (Isset ($_session[' user_id ')) {
To clear a session variable, set the $_session Super global variable to an empty array
$_session = Array ();
If there is a session cookie, delete it by setting the expiration time to 1 hours before
if (Isset ($_cookie[session_name ())) {
Setcookie (Session_name (), ", Time ()-3600);
}
Use the built-in Session_destroy () function to invoke the undo session
Session_destroy ();
}
Location header redirects the browser to another page
$home _url = ' login.php ';
Header (' Location: '. $home _url);
?>

http://www.bkjia.com/PHPjc/628874.html www.bkjia.com true http://www.bkjia.com/PHPjc/628874.html techarticle Session in PHP is a very important thing, like our user login is generally used to the session of this thing, compared to cookies, the session is much safer, and our property car often ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.