Monday 22 April 2013

PHP Sessions


PHP Sessions

A PHP session variable is used to store information about, or change settings for a user session.
Session variables hold information about one single user, and are available to all pages in one application.

PHP Session Variables



  • When you are working with an application, you open it, do some changes and then you close it. This is much like a Session.
  • The computer knows who you are. It knows when you start the application and when you end.
  • But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
  • A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc).
  • However, session information is temporary and will be deleted after the user has left the website.
  • If you need a permanent storage you may want to store the data in a database.
  • Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID.
  • The UID is either stored in a cookie or is propagated in the URL. 
  • A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc).
  • However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.
  • It is important to ponder if the sessions' temporary storage is applicable to your website.
  • If you require a more permanent storage you will need to find another solution, like a MySQL database.
  • 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.



Starting a PHP Session


Before you can store user information in your PHP session, you must first start up the session.


<?php session_start(); ?>

<html>
<body>

</body>
</html>


The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

storing a session variable

When you want to store user data in a session use the $_SESSION associative array.

This is where you both store and retrieve session data. In previous versions of PHP there were other ways to perform this store operation, but it has been updated and this is the correct way to do it.


PHP Code:

<?php
session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

Display:

Pageviews = 1



Destroying a Session

If you wish to delete some session data, you can use the unset() or the session_destroy() function.

The unset() function is used to free the specified session variable:


PHP code:

<?php
session_start();
if(isset($_SESSION['views']))
  unset($_SESSION['views']);
?>

You can also completely destroy the session by calling the session_destroy() function:

<?php
session_destroy();
?>

session_destroy() will reset your session and you will lose all your stored session data.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More