PHP Session

By | October 9, 2012

Session in php are easily manageable and it can be handle with easy ways.  Session is a super global variable, when a session variable is created that variable’s value can be retrieved in some other page.

Syntax: $_SESSION[‘session_name’] = “some value”;

Value obtained from textbox is stored as session value and that session value can be used anywhere in that website.

index.php
[code type=php]
<html>
<?php
if(isset($_POST(‘value’)))
{
$_SESSION[‘value’]=$_POST[‘value’];
}
?>
<form method=”post”>
Enter Session value :
<input type=”text” name=”ses”>
<input type=”submit” name=”submit”>
</form>
<a href=”unset.php”>Click here to unset session</a>
</html>

[/code]

 

session.php
Session variable “value” has been created in index.php page and we are retrieving its value in our session.php page
[code type=php]

<?php
echo “Session value in index.php is “.$_SESSION[‘value’];
?>
[/code]

 

unset.php
unset(); php function is used to delete a session, by using this function we delete the session and redirecting to index page.
[code type=php]

<?php
if(isset($_SESSION[‘value’]))
{
unset($_SESSION[‘value’]);
header(‘location:index.php’);
}
?>
[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *