Working with PHP Sessions

PHP $_SESSION is basically variable that stores temporary information about current user, for example you are browsing a website, but the remote server doesn't really know who you are, only way to identify current user assessing the site is using session, PHP $_SESSION allows us to store information about the user, which can be retrieved later from any page within the site, as long as the session is active.

How PHP Session Works?

PHP $_SESSION creates unique id for each user, there unique ids are stored in cookie and sometimes in URLs. However; the session data is stored in the server, even if there are hundred of users accessing the site, PHP session can exactly identify each user and serve them stored data, and it is considered safe too, because session information are not accessible from the client browser, and there are no easy way to hijack a session from remote computer.

Starting PHP Session

To start PHP session, we use session_start() function. It must be on the top of HTML or any text. Something like this :
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
<?php session_start(); // starts PHP session //other PHP codes and HTML ?> <HTML> </HTML>
When session_start() is called, it either starts a new session or resumes the existing session.

Storing & Retrieving Session variables

When the session is active, we can store values in PHP $_SESSION, later which can be accessed from any PHP script.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
<?php // starts PHP session session_start(); //store a session variable $_SESSION["name"] = "Saaraan"; //storing array variables $_SESSION["arr"] = array('name' => 'saaraan', 'url' => 'http://www.sanwebe.com', 'type'=> 'blog'); //retrieve and output stored session variable echo $_SESSION["name"]; ?>
Once we have stored variable, we can retrieve it anywhere within the same website, all we have to do is resume existing session we've created in above example and output the value.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<?php // start or resume PHP session session_start(); //retrieve and output stored session variable echo $_SESSION["name"]; //retrieve array vars print_r($_SESSION["arr"]); ?>

Removing or Destroying PHP $_SESSIONS

It is better to remove session variables when they are no longer needed, to remove certain variables we can use PHP unset().
PHP
  • 1
  • 2
  • 3
  • 4
<?php //removes variable called name unset($_SESSION["name"]); ?>
Or to totally destroy everything in current session, use session_destroy(). The function will completely destroy and reset current user session.
PHP
  • 1
  • 2
  • 3
  • 4
<?php //destroy everything session_destroy(); ?>
  • OMG finally a simple answer to exactly how sessions work. I know i know, its so simple now that i understand but most site blank over this info as if i should already know. Thank you so much for all your wonderful info! :D
New question is currently disabled!