Dialogue on The Couch (ii)

Source: Internet
Author: User
Tags count time interval client

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;

?>





Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.