User state management (session support) is a long-awaited new feature of PHP 4.0. In the era of PHP 3.0, programmers had to use libraries written by others to implement state management functions, or simply abandon this function. The lack of state management functionality is actually one of the most disappointing aspects of PHP 3.0. But now the situation has changed. Starting from the early beta version of PHP 4.0, user state management has become one of PHP's built-in functions.
You can use the status management function to manage all related variables from the time the user enters the website until he leaves the website (as long as the user does not leave the website, these variables can be accessed and will not change due to When the user leaves a single page and the data disappears), there is no need to store many cookies or use hidden form fields, or even store these variables in the database, causing a large load on the database server.
Once you activate status management on a page in the website, the PHP engine will start recording the user status (if the system has not started recording the visitor status for this user), or continue to record a certain A previously activated user state. To activate PHP's state management feature, you can use the following syntax:
session_start();
Once state management is activated, PHP will send a unique status code via cookie (this code will look like: 940f8b05a40d5119c030c9c7745aead9 ) to the user. At the same time, on the server side, the PHP engine will automatically generate a temporary text file with a file name corresponding to the status code (such as: sess_940f8b05a40d5119c030c9c7745aead9). This file will be used to store the programmer's status record of this user. All variables registered in .
When it comes to user state management, the most commonly used example is a page access counter (access counter): Now I will start to teach you how to write PHP program code.
Special Note
Before you try to activate the user status record, you must not output any content (no spaces, TAB or even newlines, etc., nor any HTML tags, no content) ) to the browser. This is because the state management related functions will send HTTP header information to the browser. If other content has been output before sending the HTTP header information to the browser, the system will generate an error message.
If the user's status management has not been activated, the following line of program will activate the user's status management:
session_start();
Next, register a variable named count:
session_register('count');
Once you register a variable, PHP will automatically maintain the value of this variable for you during the entire browsing process from the user entering the website to leaving the website. You can Get these registered variables. The newly registered variable does not have any value assigned to it, but once we increment the value of the count variable, its value will be 1: