Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Session in PHP

In this article we will see some operations with session in PHP.

Today, we are going to learn about session handling in php. We will learn about creating, deleting, checking value etc for session variable

Introduction

A user comes to your site and he logs into your site. The user moves to his home page. Now you designed your site so that each of your site page shows a title like Welcome .To show this, the hard and the bad way would be to either extract information every time from browser or to pass this information from one page to another(which is quite dangerous because this information would be editable and person would be able to get data of some other user without even knowing his credentials).The best way here is if we can store the user specific information until he uses the site and then destroy it when he is complete using our site. You may use sessions in various other places too.

A php session store information on the server on a temporary basis and delete the info when user leave the site. Session should be used to store small information. For bigger data you must make use of database like mysql. Session information is stored separately for each user. This is ensured by each session having a unique UID which separate it from other sessions.

First we are going to learn how to start a php session

Listing 1: Starting a php session

<?php session_start(); ?>

<html>
<head>
	<title>MrBool Development Tutorial</title>
</head>
<body>

</body>
</html>
  1. We start a new session making use of function php session_start();
  2. Make sure that starting session is placed before the html tag
  3. Here we user session will be maintained with server and a UID will be assigned for this particular user session.

Storing and retrieving session information

We can easily store and retrieve information in session variable a shown in below example

Listing 2: Storing and Retrieving session variable

<?php
session_start(); 
$_SESSION['special'] = 'csanuragjain'; // store session data
echo "Special Value = ". $_SESSION['special']; //retrieve data
?>

Let's to the explanation the code:

  1. First we started the session.
  2. We stored a string named csanuragjain inside the session variable. The key for this value in session array is ‘special’
  3. Now we retrieve the value which has been stored in the session and display that making use of the echo statement
  4. Here we displayed using $_SESSION[key]. We wanted to display value associated with special so we used $_SESSION['special'].This will display csanuragjain.

Now we will learn about the function isset. This is used to check if a particular session variable already exists or not. We explain with the help of a very simple example

Listing 3: isset function

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

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

Let's to the explanation the code:

  1. First we start the php session.
  2. We use the isset function to check whether the session variable ‘Views’ has already been set or not. If this is set(which means user have already visited our site in this active session) then we increase the value of session variable views by 1.
  3. If the session variable ‘views’ is not set then we will set the value of session variable ‘views’ as one since this is the first time he is visiting the page
  4. Now we display the number of time he visited this particular page using $_SESSION['views'].
  5. I will explain the above with simple example too. Suppose you have coded this in your website. Now user visits this page. What this page does is start the session and check if a session variable named views has been set up or not. Since it has not been set up so the session variable ‘Views’ is given a default value of 1 and the same is shown to user. Assume this user again visit the same page in same session. Now the script check if a session variable named ‘views’ exist or not. Since it exist , it will increment the value of this variable by 1.So now the new value of session variable ‘views’ would be 2 and the same will be displayed to the user

Cleaning your session variable

Assume that you are using the session to store some shopping cart information of user. Now if user selects a product then you will add that to the session variable. Now suppose he completed his shopping and all transaction and billing part is complete. So now you might not need data associated with shopping cart. We will learn how to do that in the below simple example

Listing 4: cleaning session variable

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

First we started the session, after this, we check if the session variable named shopping, which contained shopping related information for the user, exists or not. If it exists then we use the unset function and pass the session variable which we want to unset.

Remember it will only unset the value linked with key ‘shopping’.If this session contains other keys too like username then there value will remain there.

Deleting a session variable

When a user has completed visiting your site and wants to logout then you had to destroy his/her session. This is also important from security point of view

Listing 5: deleting user session

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

Let's to the explanation the code:

  1. First we start the session
  2. To completely destroy this session and all values associated we use the method named session_destroy()
  3. Be careful when you use this function as this will completely delete all session information

This is all for this article. Hope you enjoyed the article. I will try to come up with some more informative topics. See you next time



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login