How to use session in PHP to save user logon information? session user logon _ PHP Tutorial

Source: Internet
Author: User
How to use session in PHP to save user logon information and session user logon. In PHP, how to use session to save user login information is very important in php, like our user logon, we usually use session in East PHP. how can we use session to save user login information? session user login

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, we often use session in our shopping cart to save temporary records.

Use session to save logon information on the page

1. database connection configuration page: connectvars. php

<? Php // database location define ('Db _ host', 'localhost'); // username define ('Db _ user', 'root '); // PASSWORD define ('Db _ password', '000000'); // database NAME define ('Db _ name', 'test');?>

2. logon page: logIn. php

<? Php // Insert information related to database connection require_once 'connectvars. php '; // when a SESSION session_start (); $ error_msg = ""; // if the user is not logged on, $ _ SESSION ['User _ id'] is not set, run 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']); 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 ['use Rname']; $ 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 has logged on, go directly to the logged on page $ home_url = 'loged. php '; header ('Location :'. $ home_url) ;}?>      Mismatch-Log In    
     Msimatch-Log In
 <? Php if (! Isset ($ _ SESSION ['User _ id']) {echo'

'. $ Error_msg .'

';?> <? Php }?>

3. logon page: loged. php

<? Php // enable session session_start () before using the variable value stored in the session (); // use a SESSION variable to check the logon status if (isset ($ _ SESSION ['username']) {echo 'you are Logged '. $ _ SESSION ['username'].'
'; // Click "Log Out" and go to the logOut page to cancel echo 'log Out ('. $ _ SESSION ['username']. ')';}/** on the logged-on page, you can use a user's session such as $ _ SESSION ['username'] and * $ _ SESSION ['User _ id'] to query the database. how many things can be done? */?>

4. logOut session Page: logOut. php (redirection to lonIn. php after logOut)

<? Php // you must start the session before accessing the session variable session_start () even if you log out (); // use a SESSION variable to check the logon status if (isset ($ _ SESSION ['User _ id']) {// to clear SESSION variables, set the $ _ SESSION Super global variable to an empty array $ _ SESSION = array (); // if a SESSION cookie exists, you can set the expiration time to the previous one hour to delete it if (isset ($ _ COOKIE [session_name ()]) {setcookie (session_name (),'', time ()-3600);} // use the built-in session_destroy () function to call the undo session session_destroy ();} // The location header redirects the browser to another page $ home_url = 'login. php '; header ('Location :'. $ home_url ); ?>

All right, the above code is all the content of this article. the code is very simple. there are comments and text instructions in many places. if you have any questions, please leave a message. I will reply to you immediately. Thank you!

Semi session is a very important thing in php. for example, we usually use session for user login...

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.