Session and storing, retrieving values
by rajesh[ Edit ] 2009-10-29 09:38:10
A session is used in php to temporarily store values as variables in the server.
A session value once stored will be available until
- the browser is closed
- session timeout is reached
- you remove the value from the session.
Unlike a cookie, the value is not stored on the users computer.
Starting a session:
You have to start a session to insert values in to the session
<?php
session_start();
// this function should be the first line on you page where you are to store the value.
?>
Storing a value:
Now we are to start a session and set a value in to a variable from a php page (say x.php).
<?php
session_start();
// setting values in to the session as variables
$_SESSION['test']='my value';
?>
Retriving a Value:
Consider we need that value in a different page (say y.php).
The code will be as follows
<?php
session_start();
// session_start should always be the first line of code in you php page
//retrieving or getting the stored session value
echo "Stored value is ".$_SESSION['test'];