Papermashup

Subscribe


Tweets


"@KBedders @elerichardson I'm Free!!!!!! Call / Whatsapp me!!!"

@ashleyford 2 weeks ago

"@KBedders moomin!"

@ashleyford 3 weeks ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Using PHP Sessions

AshleyAshley

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();

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 4
  • manoj
    Posted on

    manoj manoj

    Reply Author

    Really It’s very useful


  • ClikPals Network
    Posted on

    ClikPals Network ClikPals Network

    Reply Author

    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.


  • raymond trangia
    Posted on

    raymond trangia raymond trangia

    Reply Author

    ah ok


  • mera
    Posted on

    mera mera

    Reply Author

    after all this time only these two lines bah