Application of PHP User state management function _php Tutorial

Source: Internet
Author: User
User status Management (session support) is PHP 4.0 a new feature that has been expected for a long time. In the era of PHP 3.0, programmers had to use a library that other people wrote to implement state management functions, or simply give up on this feature. The lack of state management functionality is actually one of the most disappointing parts of PHP 3.0. However, now that the situation has changed, from 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 feature to manage all relevant variables in the period from the time the user enters the site until it leaves the site (as long as the user does not leave the site, these variables can be used, not because the user leaves a single page to cause the data to disappear), without the need to store many cookies Or even storing these variables in a database, using hidden form fields, causes a heavy load on the database server.
Once you activate state management on a page in the site, the PHP engine will start recording the user status (if the user is not already logged on to the audience), or continue to record a previously activated user status. To activate the state management feature of PHP, you can use the following syntax:
Session_Start ();
Once the state management is activated, PHP transmits a unique status code via a cookie (this code looks like the 940f8b05a40d5119c030c9c7745aead9) to the user, while on the server side, PHP The engine automatically generates a staging text file (for example: SESS_940F8B05A40D5119C030C9C7745AEAD9) with a file name corresponding to the status code, which is used to store all variables registered by the programmer in the user's state record.
When it comes to user state management, the most commonly used example is a page access counter (Access counter): Now I'm going to teach you how to write PHP program code.
Special attention
Before you try to activate the user status record, you must not be able to output anything (no spaces, tabs or even line breaks, no HTML labels, no content) to the browser. This is because the state management-related function sends the HTTP header (header) information to the browser, and the system will receive an error message if something else has been output before the HTTP header information is sent to the browser.
If the user's state management has not yet been activated, the following line of the program 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 for you throughout the browsing process from the user's access to the site until it leaves the site, and you can take these registered variables at any time. The newly registered variable does not specify any value to it, but once we increase the value of the count variable, it will be 1:

http://www.bkjia.com/PHPjc/445197.html www.bkjia.com true http://www.bkjia.com/PHPjc/445197.html techarticle User Status Management (session support) is PHP 4.0 a new feature that has been expected for a long time. In the era of PHP 3.0, programmers had to use a library written by other people to implement ...

  • 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.