First session
One of the standard examples of how the session is working is the click Count Application-a simple session-based counter that initializes a variable when you first visit a Web page and increases its count every time you reload the page. The code is as follows:
<?php
Initializes a session session_start ();
Register a Session variable Session_register (' counter ');
?>
Each session in PHP4 begins with the call to the Session_Start () function, which checks whether a session exists or creates a new one if it does not exist. Next, you register a variable with the Session_register () function, which will survive throughout the session--in the example above, the variable is named "Counter," and no value is assigned to it.
Now, let's add a few lines of code to the above example and click the Count to start working:
<?php
Initializes a session session_start ();
Register a Session variable Session_register (' counter ');
Add counter $counter ++;echo ("You have visited this page $counter times! Don ' t you have anything else to doing, you bum?! ");
?>
Give it a try! When you reload the page every time, the value of the counter is incremented, which shows how the variable is saved in session.
Why do we have such a situation for the FA? Well, every time a session is created, a session of cookie[called PHPSESSID] is created in the client system and assigned a random number, and a similar entry is created on the server side that contains the variables registered in the session. The communication between the client and the server is implemented through the identification number (ID) of the session with the same name, so that different variables can be saved throughout the sessions.
Let's look at a more complicated one. Looking at this example, it shows a timer that uses the session variable, which tells you how long it took to reload the page.
<?php
Session_Start ();
The session variable is used to save the counter session_register (' counter ');
The session variable is used to save the last loaded time value
This value is saved to compare the different session_register (' timeatlastload ') of two times;
Current $timenow = time ();
Increase Count $counter++;
Compute two time interval $timelapsed = $timeNow-$timeAtLastLoad;
Display information if ($counter > 1)
{
echo "<b>it ' s been $timeLapsed seconds since you last viewed
This page.</b> ";
}
Else
{
echo "<b>first time?" Reload this page to the
Session works!</b> ";
}
$timeAtLastLoad = $timeNow;
?>