Where your adventure begins
Full Service Web HostingNetObjects Fusion 7 Websites Portal



Fact: HTML is stateless. Terrific....but what does that mean? Basically, HTML has a what "happens here, stays here" attitude about things. Once served up by the web server to your browser, it's what you see is what you get. Look at the source code for an HTML page. That's all there is.

The exception is forms. Forms are used to collect input from a visitor and pass that input along to the form action. The action, in most cases is another HTML page or a script sitting in a cgi-bin on the web server.

But, once the form does its job, the information goes away and you can't use it again without retrieving it from wherever you sent it to. A database, file or whatever. Now, most of the time that's just fine. You've done all you planned on doing with the info.

But, what if you need to use that info to do something like personalize your site for a visitor.

This case study will show you how to do that. How? By using PHP sessions. Sessions can also be used for more complex tasks like shopping carts or membership systems.

What's a Session?

Let's define it as "what happens with certain information when you visit a web site that uses PHP sessions as you move through the site. Either until you leave the site and close your browser or the PHP code in the site ends the session based on something you do/don't do on the site."

Starting a Session

The code is simple:

Starting a PHP Session

<?php session_start(); ?>

This function starts a session. It also keeps a session "alive". Any page in your site that uses session variables must contain this code....not just the page where the session begins.

The rules: a "session start" must go on the very first line of your HTML page. Before everything else. Also, no blank lines can appear above the session start code. How you get the code onto a page depends on the version.

Fusion 7.5: A new feature in 7.5 is some additional HTML insertion points. The one we'll use here is Start of Page. You can use the Master Border Page HTML or the Layout Page HTML.

IMPORTANT: there is a bug in Fusion 7.5. Do not use the Master Border Start of Page tab. It will insert a blank line above the session start and break your code. This will be fixed in AU #2.

Fusion 7 (or earlier): BitMotion makes a component called First Line. As a gotFusion member, you can receive this component free as one of the components included with your membership. First Line can be used to insert code at the top of your page. The alternative, which is NOT recommended, is to insert the code directly in HTML View as shown below.

Once again, this is not recommended. Inserting code directly in HTML View for any purpose can destabilize your site file. There are many insertion points where you can place custom code in Fusion. Use those instead.

Setting and Registering a Session Variable

How you accomplish this depends on whether or not register_globals is On or Off in the PHP.ini file on the web server. See the tutorial on PHP Register Globals and Form Variables for info on this setting. It's probably "Off", but check with your host. The two methods are shown below.

Register Globals "Off" - This is actually the easiest of the two methods. You simply assign the variable a value. Let's suppose you used a form to collect your visitors first name and the Name of the input is FirstName. You could use the following

$_SESSION['FirstName'] = $_POST['FirstName'];

Register Globals "On" - The code below accomplishes the same thing as the example above.


// if you want to register more than one. Separate with commas:

     session_register('FirstName', 'LastName');

Note: on the page receiving the form data, the input can be used as its received - as a $_POST array element. But, to preserve and use the info throughout the session, you assign its value to the session array.

Another note: pre-PHP 4.1, you must use the $HTTP_SESSION_VARS array instead of the $_SESSION array. Starting with PHP 4.1, you may use either one. However, the "$HTTP..." named arrays are deprecated and may be discontinued in future PHP releases.

Warning: if Register Globals is OFF and you use $HTTP_SESSION_VARS or $_SESSION, do NOT attempt to use session_register().

Using a Session Variable

Once you've registered a session variable, you may use it in a script. The value of the variable will remain intact until you change it or get rid of it. We're going to use it as is. Once again, there's more than one method.

Register Globals "Off"

$FirstName = $_SESSION['FirstName'];

     echo "Hello $FirstName"; //converted to short form variable
// or
     echo 'Hello $_SESSION['FirstName']';

Register Globals "On"

     echo "Hello $FirstName";


Note: This "Echo" statement can be placed on any page with the session start code at the top and your visitors name will appear on the page wherever you place it using a text box or table cell and Ctrl+T.

Unsetting a Session Variable and ending a Session

When you're finished using a session variable, you have more choices. It will go away naturally when the visitor leaves your site or closes their browser. Or you can de-register the variable. Once again, there are two methods.

Register Globals "Off"

$unset($_SESSION['FirstName']); //repeat for each variable


Register Globals "On"

     session_unregister("FirstName"); //a single variable

     session_unset(); //all session variables


Ending a Session



Warning: if Register Globals is OFF and you use $HTTP_SESSION_VARS or $_SESSION, do NOT attempt to use session_unregister().


This example took you through the process of creating a session, setting up session and using session variables and then unsetting those variables and then ending the session.

It's also possible to store session information in a cookie. That's another tutorial.

this tutorial written and maintained by Chuck Joslin

Return to the TOP of this page

Posted 30 Aug, 2003

|  Fusion  |  Web Design  |  Hosting  |  Resources  |  gotFusion Store  | 

Problems with this page?  

All content copyright © 2002-2003 gotFusion LLC. The name gotFusion and the gotFusion ® logo are registered trademarks of gotFusion LLC
Copyright, legal notice & privacy statement