Sessions in php

I always try to provide good information to beginners in php because these small looking problems may be big problem for a beginner. Session are one of them.
We use sessions when we get to a stage where our website need to pass along user data from one page to another.

A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which requires data(the user’s selected product) to be remembered from one page to the next.

Using session you can send information between one page to another.However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions. 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 web page.

Before you start using sessions in php below is a simple script that you should place at the beginning of your PHP page to start using a PHP session.otherwise you can not access the session value.
PHP Code:
<?php
session_start(); // start up your PHP session!
?>

Now you can make a session variable like this.
<?php
$_SESSION['views'] = 1; // store session data
echo “Pageviews = “. $_SESSION['views']; //retrieve data
?>
Basically php stores session data in an array like post or get array.So when you declare any session variable php automatically add that value in session array and you can access that value from that array anywhere on your site.you can print session array like this.
<?php
print_r($_SESSION);
?>

If you want to check that a session variable exists or not you can use php’e isset function like this.

<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;

echo “views = “. $_SESSION['views'];
?>

Cleaning and Destroying your Session

Although a session’s data is temporary and does not require that you explicitly clean after yourself, you may wish to delete some data for your various tasks.

Imagine that you were running an online business and a user used your website to buy your goods. The user has just completed a transaction on your website and you now want to remove everything from their shopping cart.
PHP Code:

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

You can also completely destroy the session entirely by calling the session_destroy function.

<?php
session_start();
session_destroy();
?>

session_destroy() will completely destroy all session data from your site.
Remember to place session_start() function at the top of page.You can write this anywhere in your script but sometimes it makes problem for you so its good to place session_start() function at the top of page.

You can make multidimensional arrays of sessions like normal arrays in php
If you want to learn about multidimensional arrays then follow this link.

One Response to “Sessions in php”

  1. Hans Hulslander Says:

    Intimately, the post is actually the greatest on this laudable topic. I fit in with your conclusions and will eagerly look forward to your forthcoming updates. Just saying thanks will not just be enough, for the fantasti c lucidity in your writing. I will directly grab your rss feed to stay informed of any updates. Good work and much success in your business efforts!


Leave a Reply

XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>