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: <?phpsession_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..
I'm a newbie in php,before today I'm puzzuled with session,now I understand it.Do you have articles about cookie?
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.
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
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']; ?>