HTMLGoodies
The ultimate html resource
Earthweb.com


About the Double-Underlined Links


Become a Partner




Search Clipart.com:



internet.commerce















HTML Goodies : Beyond HTML : PHP: PHP Tutorial: Sessions

HTML GOODIES TO GO NEWSLETTER


Other Related Newsletters

PHP Tutorial: Sessions


By Vince Barnes

There is a relationship between Sessions and Cookies -- they serve somewhat the same purpose, and are, to a certain extent, usable interchangeably.  Sessions, which were integrated into PHP in version 4 of the language, are a means to store and track data for a user while they travel through a series of pages, or page iterations, on your site.

The most significant differences between the two are that cookies are stored on the client, while the session data is stored on the server.  As a result, sessions are more secure than cookies (no information is being sent back and forth between the client and the server) and sessions work even when the user has disabled cookies in their browser.  Cookies, on the other hand, can be used to track information even from one session to another by setting it's time( ) parameter (see http://www.htmlgoodies.com/php/p14cookies.html)

How Sessions Work

Sessions in PHP are started by using the session_start( ) function.  Like the setcookie( ) function, the session_start( ) function must come before any HTML, including blank lines, on the page.  It will look like this:

<?php
session_start( );
?>
<html>
<head> ....... etc

The session_start( ) function generates a random Session Id and stores it in a cookie on the user's computer (this is the only session information that is actually stored on the client side.)  The default name for the cookie is PHPSESSID, although this can be changed in the PHP configuration files on the server (most hosting companies will leave it alone, however.)  To reference the session Id in you PHP code, you would therefore reference the variable $PHPSESSID (it's a cookie name; remember that from Cookies?)

Your sharp mind may be wondering what happens when you come to the second pass through your page and reach the session_start( ) function again.  PHP knows that there is already a session on progress and so ignores subsequent instances of the session_start( )   -- phew!!

Using Session Data

Having established a session, you can now create, store and retrieve information pertaining to that session.  You might want, for example, to keep track of items in your visitor's shopping cart.  Information for sessions is stored in a special directory on the server; the path of that directory is specified in the server's PHP configuration files.

Information to be stored for a session is kept in session variables.  Session variables are created by registering them for the session, using the session_register( ) function.  To use that information (on any page iteration in the session) you simply reference the variable just like you would any other variable.  Here's an example:

<?php
session_start( );
?>
<html>
<head>

<title>Using a session variable</title>

</head>

<body>

<?php

print "Welcome to session number: ";

print $PHPSESSID;

?>

<br />

<?php

session_register("username");

$username = "Goody";

print "Your name is: ";

print $username;

?>

</body>

</html>

In this example we have created a session and displayed the session number.  We then registered a session variable called username (notice the quotes around the variable's name in the call to the session_register( ) function.)

Next we assigned a value to that variable with the " = " assignment operator (remember operators from http://www.htmlgoodies.com/php/p05expressions.html?) and then displayed the value of that session variable.

We now have all the basic tools to establish a session, and to create and use variables that last through the entire duration of the session.

 

Continue to the next part of this Tutorial

Return to the Tutorial Series Index

Tools:
Add htmlgoodies.com to your favorites
Add htmlgoodies.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

IT Management Networking & Communications Web Development Hardware & Systems Software Development Earthwebnews.com

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES