Using PHP Sessions


» Follow me on Twitter

In this short post, I will try to show you how to use PHP Sessions. It’s simple and easy to do. I believe that the best way to explain this is to show an example. What we’ll try to do is register a session variable on one page (phpscript1.php) and echo the same variable on another page (phpscript2.php).

So, let’s start by first creating a session variable named ‘var’ on phpscript1.php.

Filename: phpscript1.php

session_start ();  // < - start the session
$_SESSION['var'] = 'value';  // <- register a variable

Then, we echo our session variable on another page…

Filename: phpscript2.php

session_start (); // < - start the session
echo $_SESSION['var'];  // <- echo the session variable

Summary:

As you can see, using PHP Sessions is easy and straightforward. Just always remember the following:

  1. When using sessions, always begin your PHP script with ’session_start()’
  2. Use the $_SESSION pre-defined array to register your sessions in the following manner: $_SESSION['varname'] = ‘value’;

If you have questions, feel free to post them below.

Related links:


» Follow me on Twitter


February 24, 2006 · Posted in Technology  
    

Comments

Leave a Reply

Start a Franchising Business in the Philippines