How to use session in PHP to save user login information, session user Login _php tutorial

Source: Internet
Author: User

How to use session in PHP to save user login information, session user Login


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 shopping cart often use session to do temporary record save Oh.

Use session to save page login information

1. Database Connection Configuration page: connectvars.php

<?php//Database location define (' db_host ', ' localhost ');//user name define (' Db_user ', ' root ');//password define (' Db_password ', ' 19900101 ');//Database name define (' db_name ', ' test ');? >

2. Login page: login.php

<?php//Insert connected Database information require_once ' connectvars.php ';//Open a session session_start (); $error _msg = "";//If the user is not logged in, the $_ is not set session[' user_id '], execute the following code if (!isset ($_session[' user_id ')) {if (Isset ($_post[')]) {//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 to encrypt a string one-way $query = "Select User_i D, username from Mismatch_user WHERE username = ' $user _username ' and '. "      Password = SHA (' $user _password ') ";      Query with user name and password $data = mysqli_query ($dbc, $query);        If the record is exactly one, the session is set, and the page is redirected 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

<?php if (!isset ($_session[' user_id ')) {echo '

'. $error _msg. '

'; ?> <?php}?>

3. Login page: loged.php

<?php//You must first turn on session session_start () before using the variable values stored in the session,//Use a session variable to check the login status if (isset ($_session[' username ')) {  echo ' you is logged as '. $_session[' username ']. '
'; Click "Log Out" to go to the logout page to unregister echo ' Log Out ('. $_session[' username ']. ') ';} /** 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 (redirected to lonin.php after logout)

<?php//even when logging off, you must first start the session 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 there is a session COOKIE, remove it by setting the expiration time to 1 hours before the If  (Isset ($_cookie[session_name ())) {    Setcookie (Session_name (), ", Time () -3600);  }  Use the built-in Session_destroy () function call to revoke session  Session_destroy ();} The location header redirects the browser to another page $home_url = ' login.php '; header (' Location: '. $home _url);? >

Well, the above code is the entire content of this article, the code is very simple, many places are accompanied by annotated text description, where do not understand the place welcome to my message, I will give you the first time to reply. Thank you!

http://www.bkjia.com/PHPjc/1063241.html www.bkjia.com true http://www.bkjia.com/PHPjc/1063241.html techarticle how to use session in PHP to save user login information, session user login session in PHP is a very important thing, like our user login is generally used to the session of the East ...

  • 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.