Application of User state management function in PHP

Source: Internet
Author: User
Tags array continue header session id new features variables php code variable
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:

$count + +;

By combining these program codes, our program code will do the following: Activate the user status record (if it is not already active for that user), specify a unique status code (session ID) for the user, and register a variable named count. And each time the user browses to the page, the value of the variable $count plus one, which can be used to record the number of times the user has browsed the page.

If you want to see how many times the user has viewed the page in this browsing process, we just need to print the value of the variable $count:

echo "<p> You have browsed this page $count times. </p> ";

The full Page view counter program code looks like this:

<?session_start ();
Session_register (' count ');
$count + +;
echo "<p> You've been browsing this page $count times. </p> ";
? >


If you continue to reload the page, you will find that the number of views displayed on the screen will continue to increase. In addition to registering simple variables, we can register an array into the user status record. Let's say we have the following array named $faves:

$faves = Array (' Classical music ', ' travel ', ' singing ', ' Linux ');

The practice of registering arrays is exactly the same as registering other simple variables:

Session_register (' faves ');

After registering the array, it is no different to refer to the array in the program code in the future, as long as it is simply called to use the $faves variable. If your user chooses something that he likes in a page form within a site, and you register the items through $faves array into the user's status record, you can easily display them on other pages in the site:

?
Session_Start ();
echo "What my visitors like is: <ul>";
while (the list ($v) = each ($faves)) {
echo "<li> $v </li>"; }
echo "</ul>";
? >


Just so lightly, you can show what your visitors like on the Web.

The user status record is registered with a variable that cannot be covered with a query string, for example, if the user cannot enter the following URL directly in the address column of the browser:

Http:///www.yourdomain.com/yourscript.php?count=56 to attempt to cover the value of the $count variable that was originally registered in the user status record. This is a very important security concept: Only you can register or delete variables in the user status record within your program, and other users cannot attempt to confuse the values of these variables with the query string following the URL.

To delete a previously registered user state variable, you can use the following syntax:

Session_unregister (' count ');

To remove the entire user state record variable and stop the record, use the following syntax:

Session_destroy ();

Conclusion:

The benefits of properly leveraging the user status record feature are many: it lets us not store user state data in the database, reducing the load on the database server. It also allows us to write long program code to record these user state variables through cookies (and, in this way, we don't have to spend a long time in the privacy Statement of the site to explain why we need to store 50 cookies when the user logs in) into their hard drive inside). This feature allows us to only need a cookie to store a variable (session ID), all the other information is through a sophisticated mechanism to help us record, so that our work is more simple!

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.