Session Variable

HTTP was designed to be as open as possible and one of the drawbacks of this is that the protocol is stateless i.e. there is no persistence of variables across web requests. When a request is sent to a web server for a resource, the server doesn’t know or care if the request comes from someone already on the server or from someone logging in from outside. Therefore if a web site needs to maintain state, and allow data to be shared across pages, HTML pages just won’t do it. PHP does allow variables to be passed from page to page with a query string such as this:

echo (’<a href=”next_page.php?name=Dave&job=programmer”>next page</a>’);

The page has an HTML form that invites a user to key in his or her name and occupation. These details will be passed as name-value pairs, called $name and $job to a PHP page that will store them as session variables. The first piece of code on the page, and all other pages that need to access the variables, will be:

<?php session_start(); ?>

So to go back to the HTML form page, when the page first downloaded to the user’s browser, an empty session file was created on the server and the user’s browser now has a session identifier. The user fills in the form and clicks the send button. The form variables are sent to the PHP page that will store the variables, and the code will take this form:

<?php session_start(); // This connects to the existing session
session_register (”name”); // Create a session variable called name
session_register (”job”); // Create a session variable called job
$HTTP_SESSION_VARS ["name"] = $name; // Set name = form variable $name
$HTTP_SESSION_VARS ["job"] = $job; // Set job = form variable $job

113 Responses to “Session Variable”

  1. SHANE says:

    sample ncp for diabetes

    Buy_without prescription…

  2. jordan says:

    Download@pop.Rock” rel=”nofollow”>.

    Buy rock US Charts…

Leave a Reply