Application of User state management function in PHP
Source: Internet
Author: User
The User state management (session support) is one of PHP's 4.0 new features that have been expected for a long time. In the PHP 3.0 era, programmers had to implement state management functions with other people's written libraries, or simply give up the feature. The lack of state management functionality is, in fact, one of the most disappointing places in PHP 3.0. But now the situation has changed, starting with the early beta version of PHP 4.0, User state management has become one of PHP's built-in features.
You can use the state management function to manage all relevant variables for the duration of the user from the time they enter the site until they leave the site (as long as the user does not leave the site, then these variables can be used, not because the user left a single page and cause the data to disappear, without the need to store a lot of cookies Or use hidden form fields, and even store these variables in the database, causing a large load on the database server.
Once you activate state management on one page of the site, the PHP engine starts recording the user's state (if the system hasn't started recording the visitor's status for the user), or continues to record a previously activated user state. To activate the state management function of PHP, you can use the following syntax:
Session_Start ();
Once the state management is activated, PHP sends a unique status code via cookies (which looks like: 940f8b05a40d5119c030c9c7745aead9) to the user, while at the end of the server, PHP The engine automatically generates a staging text file (such as SESS_940F8B05A40D5119C030C9C7745AEAD9) that corresponds to the status code, which stores all the variables registered by the programmer in the user status record.
When it comes to user state management, the most commonly used example is a page access counter (Access counter): Now I'm starting to teach you how to write PHP code.
Special attention
Before you try to activate the user status record, you should not be able to output anything (no spaces, TAB or even a newline, etc. spaces, nor any HTML label, no content) to the browser. This is because the state management correlation function sends the HTTP header (header) information to the browser, and the system will receive an error message if something else has been exported before sending the HTTP header message to the browser.
If the user's state management has not been activated, the following line of programs activates the user's state management:
Session_Start ();
Next, register a variable named count:
Session_register (' count ');
Once you have registered a variable, PHP will automatically maintain the value of the variable from the user's access to the site through the entire browsing process, and you can take these registered variables at any time. The newly registered variable does not specify any value to it, but once we increment the value of the count variable, its value will be 1:
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