Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Using Sessions in PHP (http://www.go4expert.com/articles/using-sessions-php-t25128/)

lionaneesh 3Mar2011 09:58

Using Sessions in PHP
 
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..

lionaneesh 3Mar2011 22:01

Re: Using Sessions in PHP
 
Thanks for accepting..

etrade123 14Mar2011 18:07

Re: Using Sessions in PHP
 
thanks for giving some important tips.

lionaneesh 15Mar2011 14:22

Re: Using Sessions in PHP
 
Quote:

Originally Posted by etrade123 (Post 80671)
thanks for giving some important tips.

Welcome and have a look at my other articles toO!!

etiergjt 21Mar2011 07:40

Re: Using Sessions in PHP
 
I'm a newbie in php,before today I'm puzzuled with session,now I understand it.Do you have articles about cookie?

eunikorn 28Mar2011 11:19

Re: Using Sessions in PHP
 
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.

muralideni 18Oct2011 16:20

Re: Using Sessions in PHP
 
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

donor 13Jan2012 11:37

Re: Using Sessions in PHP
 
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'];
?>

jamesjohn 13Mar2012 14:32

Re: Using Sessions in PHP
 
Wow..very valuable information you have shared here.
Thanks a lot.

lionaneesh 13Mar2012 14:34

Re: Using Sessions in PHP
 
Quote:

Originally Posted by jamesjohn (Post 93347)
Wow..very valuable information you have shared here.
Thanks a lot.

My Pleasure :)


All times are GMT +5.5. The time now is 16:15.