Using Sessions in PHP

lionaneesh's Avatar author of Using Sessions in PHP
This is an article on Using Sessions in PHP in PHP.
Rated 5.00 By 1 users
Every web-applications needs to transfer data from one page to another and one of the way of doing it is using sessions in PHP..

Sessions are a special type of variables used to transfer/carry data from one page to others.. In this tutorial i’ll be covering PHP version 4.1.0 and above..

Using sessions in PHP



Before using sessions in php we have to use a function to initialise ..
Code:
session_start();
The function initialises PHP Session array $_SESSION..and gets its values...Without this function the $_SESSIONS[] array cannot be used..

To assign a session a variable..
We simply use :-
Code:
$_SESSION[key] =value;
Eg :-
Code:
$_SESSION[“name”] = “lionaneesh”;
So , that’s quite a bit of theory now lets get coding..
Code:
<?php
session_start();
?>
<html>
<head>
  <title>Demonstrating the use of Session Variables in PHP</title>
</head>
<body>
<?php
$_SESSION["name"] = "lioaneesh";
print"I have set a session variable 'name' to ". $_SESSION['name'] . "<br />\n";
print"<a href=\"next.php\">Please Click Here!!</a>\n" 
?>
</body>
</html>
next.php
PHP Code:
<?php
session_start
();
?>
<html>
<head>
  <title>Demonstrating the use of Session Variables in PHP</title>
</head>
<body>
<?php
$name 
$_SESSION["name"];
print
"The name passed to the page using Session variables is $name\n"
?>
</body>
</html>
Output :-
session.php
Code:
I have set a session variable 'name' to lioaneesh
Please Click Here!!
next.php
Code:
The name passed to the page using Session variables is lioaneesh
Now that we know how to create and use sessions..
What if we want to unset a session variable or delete the Whole $_SESSION array (for cleanup purposes)..
We can unset a session variable simply by using :-
Code:
unset($_SESSION[key]);
We can destroy the session by using :-
Code:
session_destroy();
Let’s just use them in our code and see what’s happening
session.php
Code:
<?php
session_start();
?>
<html>
<head>
<title>Demonstrating the use of Session Variables in PHP</title>
</head>
<body>
<?php
$_SESSION["name"] = "lioaneesh";
$_SESSION["site"] = "Go4expert";
print"<a href=\"next.php\">Please Click Here!!</a>\n" 
?>
</body>
</html>
next.php
Code:
<?php
session_start();
?>
<html>
<head>
<title>Demonstrating the use of Session Variables in PHP</title>
</head>
<body>
<?php
$name = $_SESSION["name"];
$site = $_SESSION["site"];
print"The name passed to the page using Session variables is $name\n<br />";
print"The site name passed to the page using Session variables is $site\n<br />";

print"Unsetting name\n<br />";

unset($_SESSION["name"]);

print $_SESSION['name'];

print "Destrying the session\n<br />";

session_destroy();

?>
</body>
</html>
Output :-
Code:
The name passed to the page using Session variables is lioaneesh 
The site name passed to the page using Session variables is Go4expert 
Unsetting name
Notice: Undefined index: name in D:\wamp\www\testiNg\next.php on line 19
Destroying the session
Stay tuned for more..
0
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Thanks for accepting..
0
etrade123's Avatar
Banned
thanks for giving some important tips.
0
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by etrade123 View Post
thanks for giving some important tips.
Welcome and have a look at my other articles toO!!
0
etiergjt's Avatar
Light Poster
I'm a newbie in php,before today I'm puzzuled with session,now I understand it.Do you have articles about cookie?
0
eunikorn's Avatar, Join Date: Mar 2011
Newbie Member
Is it possible to utilize php session superglobals to have file based or database based server side session management. Is it possible to create web pages that actually do not require any rewriting of code if in future session management backend is changed ? Like in case we are switching to a load balancing proxy, we would like to switch from file based to database based session management.
0
muralideni's Avatar
Newbie Member
i need login/logout script in php mysql. I done it bt didnt work properly.Once logged in the user dont go back to the login form without click logout link. i need help someone help me............. muralideni
0
donor's Avatar
Banned
plz anybody tellme there is an error in this code????
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
?>
0
jamesjohn's Avatar
Banned
Wow..very valuable information you have shared here.
Thanks a lot.
0
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by jamesjohn View Post
Wow..very valuable information you have shared here.
Thanks a lot.
My Pleasure