Application of the PHP user status management function. User status management (sessionsupport) is a new feature that has long been expected in PHP4.0. In the PHP3.0 era, session support is a new feature in PHP 4.0 that requires programmers to use a library written by others to implement user status management. In the PHP 3.0 era, programmers must use the function library written by others to implement the state management function, or simply give up this function. The lack of status management is actually one of the most disappointing parts of PHP 3.0. However, the status has changed. since the early beta version of PHP 4.0, user status management has become one of the built-in functions of PHP.
You can use the status management function to manage all the variables related to a user's website from the beginning to the end of the website. (these variables can be used as long as the user has not left the website, does not cause data to disappear because the user leaves a single page), instead of storing many cookies, hiding form fields, or even storing these variables in the database, this causes a large load on the database server.
Once you activate status management on a page of your website, the PHP engine starts to record the user status (if the system hasn't started recording the visitor status for this user ), or you can continue recording the status of a previously activated user. To activate the PHP state management function, you can use the following syntax:
Session_start ();
Once the status management is activated, PHP sends a unique status code (the code looks like 940f8b05a40d4249c030c9c7745aead9) to the user through the cookie, and at the server end, the PHP engine automatically generates a temporary text file (for example, sess_940f8b05a40d4249c030c9c7745aead9) corresponding to the file name and status code ), this file is used to store all variables registered by the programmer in this user status record.
When talking about user status management, the most commonly used example is an access counter: now I am going to teach you how to write PHP code.
Special note
Before you try to activate the user status record, you cannot output any content (there cannot be spaces, tabs, line breaks, and other space characters, nor any HTML volume labels, nor any content) to the browser. This is because the status management function sends the HTTP header information to the browser. if other content has been output before sending the HTTP header information to the browser, the system displays an error message.
If the user's status management has not been activated, the following 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 automatically maintains the variable value for you throughout the browsing process from the user's website to the website's departure, you can use these registered variables at any time. The newly registered variable does not specify any value for it, but once we increase 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 function (if not activated for this user ), specify a unique status code (session id) to the user, register a variable named count, and add the value of the variable $ count each time the user browses the page, this value can be used to record the number of times that the user browsed the page.
If you want to view the page several times during this browsing process, you just need to print out the value of the variable $ count:
Echo "<P> You have browsed this page $ count times. </P> ";
The complete page browsing counter program code looks like this:
<? Session_start ();
Session_register ('count ');
$ Count ++;
Echo "<P> You have browsed this page $ count times. </P> ";
?>
If you reload the page, you will find that the number of browsing times displayed on the screen increases constantly. In addition to registering simple variables, we can also register an array to the user status record. Suppose we have the following array named $ faves:
$ Faves = array ('classical music', 'travel ', 'change', 'Linux ');
Registering arrays is the same as registering other simple variables:
Session_register ('fafes ');
After the array is registered, there will be no difference in the practices of referring to the array in future program code, as long as you simply call the $ faves variable. Assume that your user selects something he or she prefers in a page form on the website, and you register these items in the user's status record through the $ faves array, on other pages of the website, you can easily display these items on the screen:
<?
Session_start ();
Echo "what my visitors like is: <ul> ";
While (list ($ v) = each ($ faves )){
Echo "<li >$ v </li> ";}
Echo "</ul> ";
?>
Just so easily, you can display what visitors like on the webpage.
The variables registered by the user's status record cannot be overwritten by the query string. for example, the user cannot directly enter the following URL in the address column of the browser:
Http: // www.yourdomain.com/yourscript.php? Count = 56 attempts to overwrite the value of the $ count variable originally registered in the user status record. This is a very important security concept: Only you can register or delete the variables in the user status record in your program, other users cannot use the query strings following the URL to confuse the values of these variables.
To delete a previously registered user status variable, you can use the following syntax:
Session_unregister ('count ');
To delete all user status record variables and stop records, use the following syntax:
Session_destroy ();
Conclusion:
The user status record function has many advantages: it allows us to save user status data in the database, reducing the load on the database server. It also allows us to record these user state variables through cookies without writing long program code by ourselves (and, in this way, we do not need to spend a long time in the privacy statement of the website to explain why we need to store 50 cookies to their hard disks when users log on to the website ). This feature allows us to store only one cookie for a variable (session id). All other information is recorded through an extremely sophisticated mechanism, it makes our work easier!
Session support is a new feature that has long been expected in PHP 4.0. In the era of PHP 3.0, programmers must use the library written by others to implement the code...