Conversation posted in Changsha (2)

Source: Internet
Author: User

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 ;";
Echo "$ # @ 60; B $ # @ 62; First time here? Reload this page to see how
Session works! $ # @ 60;/B $ # @ 62 ;";


$ TimeAtLastLoad = $ timeNow;

? $ #@ 62;

Related Article

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: 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.