How to Use session in PHP to save user login information

Source: Internet
Author: User
Tags php session
This article mainly introduces how to use session in php to save user login information. It involves phpsession user logon and other knowledge points. Using session to save user login information is better than cookie security.

This article mainly introduces how to use session in php to save user login information. It involves some knowledge points such as php session user login. Using session to save user login information is better than cookie security.

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 ['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 has logged on, go directly to the logged on page $ home_url = 'loged. php '; header ('location :'. $ home_url) ;}?> Mismatch-Log In Msimatch-Log In <? Phpif (! 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!

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.