A PHP 4.0 feature that has long been expected is session support. In contrast, PHP 3.0 users have to use a third-party library or simply do not implement this feature. The lack of session support is one of the most criticized places in PHP. However, since the release of the early beta, session support became part of PHP 4.0, so the problem was eliminated.
You can use sessions to maintain user-specific variables during a user's visit to a Web site without having to set up multiple cookies, using hidden form fields, or storing information in a database that you may often want to link to.
Starting a session on a page tells the PHP engine either to start a session (if it has not been started) or to continue the current session:
Session_Start ();
Starting a session sends a authenticated string (such as 940F8B05A40D5119C030C9C7745AEAD9) to the user via a cookie, and a pro temporary file with the same name is created on the server side, such as Sess_ 940f8b05a40d5119c030c9c7745aead9. This file contains the registration session variable and its value.
The most common example of displaying session actions is access counters:
Start your PHP module and make sure that the PHP code is the first line of the file: No blank, no HTML output, and any code. The reason is that when a session function emits a file header, if a blank or HTML output is sent before the Session_Start () function is invoked, the system will make an error.
?
Start a new session if the session does not exist for the user
Session_Start ();
Next, register the count variable.
Session_register (' count ');
The registration variable tells PHP that a variable named count exists as long as the session is present. Currently, this variable has not been assigned a value. However, if you add 1 to it, the value can be assigned to 1:
$count + +;
With these points in mind, you have done the following: Start a session (if not previously), assign a session identity to a user (if no session ID exists), register a variable named count, and add $count 1 to indicate that this is the first time the user has visited the page:
To display the number of times a user has visited the page in the current session, you only need to output $count values:
echo "<p>you ' ve been here $count times.</p>";
The entire access counter code is as follows:
?
Session_Start ();
Session_register (' count ');
$count + +;
echo "<p>you ' ve been here $count times.</p>";
?>
If you reload the above script, you can see an increase in the count value. This is really exciting.
You can also register an array in a session. Suppose you already have an array named $faves:
You can register the array just like any other individual variable:
Session_register (' faves ');
Applying an array is the same as applying other individual variables, such as $faves. If users want to display their hobbies on one page of a Web site, you can register the user's hobby in the $faves session variable and output the values on another page:
?
Session_Start ();
echo "My user likes:
<ul> ";
while (the list (, $v) = each ($faves)) {
echo "<li> $v"; }
echo "</ul>";
?>
This will get a nice, concise list of user interests.
The session variable value cannot be overridden by a query string, that is, you cannot assign a new value to a registered session variable $count by typing http:///www.yourdomain.com/yourscript.php?count=56. This is critical to security: You can only modify or delete (unregistered) session variables on the server side by scripting.
If you want to completely delete a session variable, you should unregister the variable from the system:
Session_unregister (' count ');
To completely remove a session, such as pressing the logout button, the following scripts are simpler:
Session_destroy ();
Using sessions to store variable values reduces the load on the database connection and avoids the writing of nightmare-like complex code, as well as using a large number of privacy statements to explain why you send up to 50 cookies to the user during the visit. And now it's just a cookie, a variable--like a drop of water that refracts the world--nothing is simpler!
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