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..
Before using sessions in php we have to use a function to initialise ..
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 :-
Eg :-
So , that’s quite a bit of theory now lets get coding..
next.php
Output :-
session.php
next.php
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 :-
We can destroy the session by using :-
Let’s just use them in our code and see what’s happening
session.php
next.php
Output :-
Stay tuned for more..
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();
To assign a session a variable..
We simply use :-
Code:
$_SESSION[key] =value;
Code:
$_SESSION[“name”] = “lionaneesh”;
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>
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>
session.php
Code:
I have set a session variable 'name' to lioaneesh Please Click Here!!
Code:
The name passed to the page using Session variables is lioaneesh
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]);
Code:
session_destroy();
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>
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>
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


