jump to navigation

Beginners PHP Tutorials – PHP Sessions July 4, 2010

Posted by Tournas Dimitrios in PHP.

PHP sessions are used to pass data between pages without having to encode that data and put it on the URL line. Sessions are different than cookies, in that sessions are not written to the user’s hard drive like cookies are, but they perform many of the same functions, to beginners PHP Sessions sound complicated but this tutorial shows the beginner just how simple Sessions are to use.

Sessions are safer to use than cookies because a user can block cookies from being written, but they cannot block sessions.

Like a cookie, sessions gives you a way to store data while the user is visiting the site. Unlike cookies, the data is not persistent. The session information is lost when the user leaves. Session data can be made persistent by storing it to a persistent cookie, or in your MySQL database, if desired.

How Sessions Work

When you create a session, PHP assigns a unique identification number to it. This is functionally equivalent to the way that a cookie assigns a cookie name. The unique session ID insures that one user’s data is not mixed in with another’s.

How to Initiate a Session

A session must be initiated at the top of your page before any other browser output. Otherwise an error will result and the session will fail. PHP provides the Session_Start() function for this purpose.

< ? php
   session_start();  // start a new PHP session
   ? >

This is all it takes to register the new session with the server. Once this is done, you can begin storing information to the session variable.

How to Store a Session Variable

When you create a session, PHP initializes an associative array named $_SESSION. This is where you store and access session data.

< ? php
   $_SESSION['UserID'] = ‘JoeGreen’;
// store  session data
   echo "Welcome “. $_SESSION[' UserID '];
//retrieve session data
   ? >

Advanced Session Use

You can use the PHP isset function to make sure that a session has been created and that any particular piece of session data has been set.

< ? php
     echo "Welcome “. $_SESSION[' UserID '];else
       echo ‘Please log in.’;
   ? >

The first time the user arrives at the site, the UserID session variable will not be set and the user will be directed to log in before they can continue to access the site.

Once they are logged in, the session variable will be present and they will see the welcome message instead.

Ending a Session

In the example above we used session data to track login status. If your script has a logout function, you would want to destroy the session data once the user logs out.

< ? php
   ? >

If you had other session variables as well, and you want to completely remove all of them, it is quicker to simply destroy the session.

< ? php
   ? >

Sessions can be used to track anything pertaining to a user. Combine the power of sessions with PHP’s excellent array functions and you could store an entire shopping cart’s worth of data with very little effort.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s