Application of PHP user status management function
Source: Internet
Author: User
User status management (sessionsupport) is a long-awaited new feature in php (as the mainstream development language) 4.0. In the era of php (as the mainstream development language) 3.0, programmers must use the function library written by others to implement the state management function, or simply give up this function instead of SyntaxHighlighter. session support is a new feature that has long been expected in php (as the mainstream development language) 4.0. In the php (as the mainstream development language) 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 aspects of php (as the mainstream development language) 3.0. However, the current situation has changed. from the early beta version of php (as the mainstream development language) 4.0, user status management has become php (as the mainstream development language) one of the built-in functions.
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, php (as the mainstream development language) the engine starts recording the user's status (if the system hasn't started recording the visitor's status for this user), or continues recording a previously activated user status. To activate the status management function of php (as the mainstream development language), you can use the following syntax:
Session_start ();
Once status management is activated, php (as the mainstream development language) transmits a unique status code through cookies (the code looks like 940f8b05a40d20179c030c9c7745aead9) to the user, at the same time, on the server side, the php (as the current mainstream development language) engine will automatically generate a temporary text file corresponding to the file name and status code (for example, sess_940f8b05a40d20179c030c9c7745aead9 ), 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 a page access counter (access (the favorite of small websites: now I am going to teach you how to write php (as the mainstream development language) program 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 (as the current mainstream development language) automatically maintains the variable value for you throughout the browsing process from the user entering the website to leaving the website, 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, tourism, singing, Linux );
Registering arrays is the same as registering other simple variables:
Session_register (faves );
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> ";
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