PHP session processing function session, session function session

Source: Internet
Author: User
Tags php session

PHP session processing function session, session function session
PHP Session variable

When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained.

By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in the database.

The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL.

1. Start session

Before storing information to a session, you must enable the session. Php provides the session_start () function to start or continue a session. Definition:

1 bool session_start (void)

The call is as follows:

1 <?php session_start();?>2 

 

Note:

(1) The session_start () function must be located before the

(2) Whether a session is successfully created or not, the session_start () function returns TRUE, so any exception processing does not work.

(3) You can also enable the configuration command session. auto_start so that you do not need to execute this function. However, each php page starts or continues a session during execution.

2. Store or read sessions

The correct method for storing and reading session variables is to use the $ _ SESSION variable of php. $ _ SESSION is a global parameter provided by php to store and read sessions. (Note that the key of the associated array is consistent with the naming rules of common variables)

You can assign values to a stored session directly.

1 $ _ SESSION ['season '] = 'autumn ';

The preceding example sets a session element with the key name "season" and its value is "Autumn ". When reading, it is like calling a normal array element.

The following two sections of Code show how to store and read a session element.

This is the session1.php file:

1 <? Php 2/*** Created by PhpStorm. 4 * User: yuxiu 5 * Date: 2016/5/26 6 * Time: 7 */8 if (isset ($ _ POST ['submit ']) {9 session_start (); // start to create a SESSION 10 $ _ SESSION ['season '] =$ _ POST ['season']; // store SESSION data 11 header ("Location: session2.php "); // special attention should be paid to the format in header () 12 13 14} 15?> 16 <B> storage session </B> 17 

This is the session2.php file:

1 <? Php 2/*** Created by PhpStorm. 4 * User: yuxiu 5 * Date: 2016/5/26 6 * Time: 7 */8 session_start (); // create or continue a SESSION 9 $ season =$ _ SESSION ['season ']; // read session data 10 11 echo "<B> Read session </B> <br/>"; 12 switch ($ season) {13 case 'spring'; 14 echo 'is now the green spring! '; 15 break; 16 case 'summer'; 17 echo 'It's a hot summer now! '; 18 break; 19 case 'loan'; 20 echo' is now the autumn of the harvest fruit! '; 21 break; 22 case 'Winter'; 23 echo 'it's white winter now! '; 24 break; 25 default; 26 echo' sorry, the session has no data or does not exist! '; 27} 28?>

In session1.php, you first use session_start () to create a session, and then store the submitted seasonal data by assigning values to the array. Finally, you can use the header () function to directly jump to the start. In the session2.php file, you also need the session_start () function to continue a session and call the session information using the session array.

3. Destroy sessions

When a session is no longer used, it needs to be manually destroyed. Although php has the function of automatically destroying the session, this will reduce the efficiency of the program. You can use the unset () function or session_destroy () function.

<?php unset($_SESSION['season']);?>

 

Or:

<? Php session_destroy (); // note that using this function will reset the session array to lose all the stored session data?>

 

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.