Conversation posted in Changsha (2 ). Palignleft: the first session. one of the standard examples about how the session works is the click Count Application. this is a simple session-based counter. when you first access p align = left>
First session
One of the standard examples of how a session works is the click count application-this is a simple session-based counter that initializes a variable when you first access a web page, each time you reload the page, increase its count. The code is as follows:
$ #@ 60 ;? Php
// Initialize a session session_start ();
// Register a session variable session_register (counter );
? $ #@ 62;
In PHP4, each session starts by calling the session_start () function. this function checks whether a session exists. if not, a new session is created. Then, use the session_register () function to register a variable, which will survive in the entire session-in the above example, the variable name is "counter" and no value is assigned to it.
Now, let's add a few lines of code in the above example and click count to start working:
$ #@ 60 ;? Php
// Initialize a session session_start ();
// Register a session variable session_register (counter );
// Add the counter $ counter ++; echo ("You have visited this page $ counter times! Dont you have anything else to do, you bum ?! ");
? $ #@ 62;
Try it! Every time you reinstall this page, the counter value is increasing, which shows how variables are saved in the session.
Why is this happening? Well, each time a session is created, a session cookie [called PHPSESSID] will be created in the customer system and assigned a random number. at the same time, A similar entry is created on the server, which contains the variables registered in the session. Communication between the client and the server is implemented through the id of the session with the same name, so that different variables can be saved throughout the session.
Let's take a look at the complexity? Let's look at this example. it demonstrates a timer using the session variable. it tells you how long it took to reload the page.
$ #@ 60 ;? Php
Session_start ();
// The session variable is used to save the counter session_register (counter );
// The session variable is used to save the time value of the last load.
// This value is saved to compare different session_register (timeAtLastLoad );
// Current time $ timeNow = time ();
// Increase the count $ counter ++;
// Calculate the two interval $ timeLapsed = $ timeNow-$ timeAtLastLoad;
//? Information if ($ counter $ # @ 62; 1)
{
Echo "$ # @ 60; B $ # @ 62; Its been $ timeLapsed seconds since you last viewed
This page. $ # @ 60;/B $ # @ 62 ;";
}
Else
{
Echo "$ # @ 60; B $ # @ 62; First time here? Reload this page to see how
Session works! $ # @ 60;/B $ # @ 62 ;";
}
$ TimeAtLastLoad = $ timeNow;
? $ #@ 62;
Http://www.bkjia.com/PHPjc/532661.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/532661.htmlTechArticlep align = left the first session about demonstrating how the session works one of the standard examples is the click count application -- this is a simple session-based counter, in your first visit...