Tricks for dealing with PHP Sessions


PHP sessions allow developers to create variable that can be used throughout the entire user session. Instead of passing variable data around and risk loosing it, you can simply define a session variable and all your pages will be able to access that variable.

To use session variables in a page, it has to have the following statement at the top right after session_start();

after you have initialized the session, you can assign and use your session variables as follows:

$_SESSION[variablename] = $variable;

or you can retrieve the value of a session variable, even if was assigned in a different script as follows:

echo $_SESSION[variablename];

Once you are done using the session variables you can destroy them using the following code snippet:

$_SESSION = array();

session_destroy();

session_unset();

For more on using PHP sessions, visit PHP Session variables

Leave a Reply