Use your left/right keys to browse tutorials
Using PHP Sessions

Using PHP Sessions

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on August 3, 2010

When you get to a stage when you need to pass user data from one page to another you have the option to do this without the user knowing by using PHP Session variables.

Sessions are basically server-side cookies which have a corresponding client side cookie that contains a reference to its server-side counterpart. When a user visits a page, the client sends the reference code to the server, and PHP will then match that reference code to a server-side cookie and load the data in the server’s cookie into the $_SESSION superglobal.

Setting Sessions

In order to use the $_SESSION superglobal we first need to start a session by calling the session_start(); function. once this is done we can set a session variable as shown below.

session_start();  

    $_SESSION['name'] = 'Ashley';

echo "My Name is ". $_SESSION['name']; 

Destroying a session

Session variables only last as long as the user has their browser open so a session is automatically destroyed when they close their browser unless you edit your php.ini file. We can however destroy our session as shown below.

session_start();  
  unset($_SESSION['name']); 

Destroy all sessions

session_start();  
session_destroy();


More tutorials from Papermashup
Comments
4 discussions around Using PHP Sessions
  1. manoj says:

    Really It’s very useful

  2. Hey,

    This is some good stuff you have here. I want to display these notifications on my home page using growl and PHP sessions. What I want is to display a notification when someone new arrives at my homepage and not display it to regular users.

    Could you please expound on the PHP sessions tutorial and help me with this.

    Thanks.

  3. raymond trangia says:

    ah ok

  4. mera says:

    after all this time only these two lines bah





Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.