Senin, 23 Maret 2009

PHP Session Tutorial

Feb14 in Design & Coding by David Shaw
Are you at the stage where your website needs to pass information from one page to another? If so then you might want to learn about PHP Sessions.


What are PHP Sessions?

A PHP session allows you to store information on the server for use on later pages, for example a username or shopping cart items. They can also be used in our PHP Login Script Tutorial.

PHP sessions are not permanent, so once the user leaves your website the information is lost. So you need to decide whether PHP Sessions are the best option for your site.

Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users’ data from getting confused with one another when visiting the same webpage.

If you are sure that you want to use PHP sessions then keep on reading, as we explain how to use PHP sessions on your website.

Starting a PHP Session

Before you can store any information in your PHP session you need to start the session. You start the session using the following code:

view plaincopy to clipboardprint?
session_start();
?>
This needs to be at the very beginning of the code in your web page, before any HTML tags.

Storing Session Variables

Now that you can start a PHP Session, you will want to store information in it, that can be passed between the web pages.

The code below stores the value ‘david’ in the ‘username’ session:

view plaincopy to clipboardprint?
session_start();
$_SESSION['username'] = david;
?>
Retrieving Session Variables

We have now stored the value ‘david’ in the ‘username’ Session Variable. You will most likely want to output or use this variable, it can be output using the following code:

view plaincopy to clipboardprint?
session_start();
echo "Username = ". $_SESSION['username'];
?>
You can now store and retrieve PHP session variables.

Cleaning Sessions

PHP Session variables are only temporary, but you may still want to clean them out during the session. For example, if you are using your session variables to store shopping cart information, you may want to remove the items after the order is placed.

We will continue from the previous examples and clean the ‘username’ session, this can be done using the code below:

view plaincopy to clipboardprint?
session_start();
unset($_SESSION['username']);
?>
This will clear any information held inside the session variable.

Destroying Sessions

If you want to completly destroy the session, such as a user logging out. Then you need to use the following code:

view plaincopy to clipboardprint?
session_start();
session_destroy();
?>
WARNING: By destroying the session you will lose all information stored inside the session.

You can now create, store, retrieve, clean and destroy PHP sessions.

If you have any problems please feel free to comment on this post and I will try to answer them as quickly as possible.

Where Next? You could now try using these PHP Sessions inside a PHP Login Script and then learn how to encrypt your PHP login script.

Tidak ada komentar: