The Basics of PHP Sessions

Learn to Work with a Session Variable in PHP

Ana Kirk
Before Working with PHP Sessions

This tutorial on the basics of PHP sessions assumes a knowledge of extensible hypertext mark-up language (X)HTML and the fundamentals of PHP. You should know how to code advanced forms without relying on wysiwyg (what-you-see-is-what-you-get) web page creation software. You should know the syntax of PHP, be familiar with SQL, comfortable with loops, conditionals, operators, functions, and variables, including arrays.

The Valuable Session Variable

Web pages are "stateless," meaning that data from one page doesn't carry over to another even when those pages are hyper-linked and part of the same site. The use of PHP sessions is one way of making data available to multiple pages. When you log in to a site, you have options that you don't see when logged out; those options appear on every page to which you navigate while logged in. This is one example of when PHP sessions are used.

The snippet of PHP code below is part of a script I wrote to allow visitors to submit comments/questions pertaining to a specific piece of content. The site contains many articles and stories and each comment needs to be associated with a specific piece, identified by a unique number (story_id) which, of course is the primary key in the table storing the content. When the user enters his comment into the form which is "handled" by the script, he receives notification of whether his submission was successful or unsuccessful. If unsuccessful, he's given the reason/s why and allowed to try again. During his first attempt, I had the script retrieve the content's id (story_id) and place it into a hidden form field to associate every comment with a specific piece of content.

Once the user clicks the "submit comment" button, the state of the web page changes. If the content's id number that was captured on the "original" page where he read the piece isn't carried over, comments that aren't associated with specific content would enter the database. Remember, the web is stateless. The original submission was one state; subsequent attempts occur at different states. The content's id number from the first state must be carried over to any subsequent states to achieve desired results.

I assigned the content id number to a session variable so I could reference it from any other "state" or page, causing that data to be available to any page I need. The most important things to remember is that every page needing access to the data should start with the session_start() function. Also, notice how I had to register the session variable on the first page (STORY_ID); but, I only had to "call" it when when needed for other pages.

PHP Session Variable Example Code

Please note that neither PHP nor HTML tags show due to a stripping of them during the upload of AC content for security reasons. Therefore, I simply say, "Opening/closing paragraph tag," for example.

opening paragraph tag opening input tag type tag='hidden' name='story_id' opening PHP tag session_start(); session_register("STORY_ID"); $_SESSION['STORY_ID']=$_GET['story_id']; echo "value='" . $_SESSION['STORY_ID'] . "'" . " /closing input tag closing paragraph tag"; ? closing PHP tag

Referencing the PHP Session Variable:

echo 'opening paragraph tag opening input tag type="hidden" name="story_id" value="' . $_SESSION['STORY_ID'] . '"' . ' /closing input tag closing paragraph tag';

Source:

AAS in Web Development

Published by Ana Kirk

Ana Kirk is an emergency medical technician (EMT) and part-time web developer. She is also a back-up translator and author of study materials for a Christian ministry.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.