SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP

Starting , storing and destroying Session Variables in PHP

Sessions are used in PHP in various types of applications to pass data and maintain state of the user. If you are using any membership system where user has to login then after verification we have to keep the login status throughout, so we can maintain the authenticity. To do this we have to use session variables as we will store the userid inside this. Similarly for a shopping cart script we have to store the selected items of the user in session variable and display it to the visitor when required. Sessions are the unique link between the user and the server so all actions of the user can be personalized to the user and handled by the server.

We will study how to start a session, how to store variables in a session, how to destroy a session and other related functions for session handling with examples.

PHP Scripts

Here in many scripts we have used sessions and you can use them as an example. Member signup and login script uses them very often. You can download different scripts and read the code to understand how the sessions are used.

Please note that session are handled differently in PHP 5 and above so we will stick to this version only. If you are using PHP 4 then there is small difference in functions ( or syntax ) but the basic concept remains same.

Starting a session

We have verified that user entered login id and password is matching so we will give access by keeping the user id in session variable. Here is the code.

session_start();
$_SESSION['userid']=$userid;

Related Tutorial
Session ID
Session Array
PHP Cookies
Note that first line session_start(); this line has to be at the top of your page or this function is to be used before we send any data to the browser. The second line actually stores the data to the session variable, this can be any where within the page. The variable $userid stores the user id ( say bigstar ) now the same value is available in our session. Now let us see how to display or use this session variable.

Displaying or using a session variable

As the member has logged, its user id we can display by showing a welcome message. Here is an example.

echo “Welcome $_SESSION[userid]”;

Note that we must use the function session_start() at the starting of the page ( before sending any output to browser ) to use any session variables. We can check the existence of session with userid at every page level which are supposed to be used by logged in members.

if(!isset($_SESSION['userid'])){
echo "Sorry, Please login and use this page";
exit;
}

As you can see we have used isset function to check the presence of session variable storing userid. If the variable does not exist then we are displaying a message asking the user to login and then terminating the script by using exit command. This part of the script you can see in detail at our login and logout script.

Displaying all the session variable.

We can store different data in session variables. In our example we have used only one ( that is $_SESSION[userid] ) but we can store other data also. Let us add name to the session variable like this.

$_SESSION['name']="Ronny";

Now we can display all the elements of the session array and see what are the session variables created and what data they store.

while (list ($key, $val) = each ($_SESSION)) {
echo "$key -> $val <br>";
}

you will get a list like this.

userid -> plus2net
name -> Ronny

Destroying the session

To destroy or delete the sessions we can use these two commands. It is advisable to use these commands in this sequence to destroy remove sessions in PHP.

session_unset();
session_destroy();

Important : Don't forget to give session_start() command at the staring of the page.
You can test all the above code to learn and understand how the session works by running these sample codes at your system. Open the index.php file and rest you can easily understand.

Assigning unique number to the Session by using session_id

Download the ZIP file for this session management examples

Number of User Comments : 10


Google+
James Abuda23-06-2009
The script is very usefull, thanks for that script.

actually plus2net site can help me in my thesis..
Kanwar15-09-2009
Frankly admitting, I have been successful after one year of search for a good script like this.
wahid14-01-2010
Thats a great website.
Guru19-01-2011
Thanks Dude you have done a great job...
M.elShabrawy10-07-2011
thanks ,good information
session is good for applying web security techniques
dave03-01-2012
Very good php scrip; works well for me.
Thanks.
faizan03-02-2012
thanxxx man u did a great job ... u help a lot.. plz continue this work....
malay27-02-2012
its very productive for developer like me.its really help me doing difficult work in a simple way.......
Omar02-08-2013
You did a great job and easy to understand because I am learning PHP....very very helpful....Thanks.
Gandharv13-08-2013
I am storing some value in array dynamically & inserting that array in session variable. Once the page get refreshed, session value are refreshing.

How to retain the value in session after refreshment?



Thanks
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer