PHP session processing function session, session function session_PHP tutorial

Source: Internet
Author: User
Tags php session
PHP session processing function session, session function session. PHP session processing function session, session function sessionPHPSession variable when you run an application, you will open it, make some changes, and then close it. This is like a 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 
 2 3 4 

Note:

(1) the session_start () function must be locatedBefore the tag, that is, the function must be called before any output. when you write a program, you often enter a space or press enter, and an error is reported. Special attention should be paid to this. (I was pitted)

(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
 16Storage session17
 18 select the data to be set: 19

This is the session2.php file:

1
 Read session

"; 12 switch ($ season) {13 case 'spring'; 14 echo 'is now 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.

 

Or:

 

When running an application, you can open it, make some changes, and close it. This is like a session. ...

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.