session_start();
$userid="plus2net";
$_SESSION['userid']=$userid; // created session variable
Note that first line session_start(); this line has to be at the top of your page or this function is to be used before we send any data to the browser. The second line actually stores the data to the session variable, this can be any where within the page. The variable $userid stores the user id ( say bigstar ) now the same value is available in our session. Now let us see how to display or use this session variable.
echo "Welcome $_SESSION[userid]";
Output is here
Welcome plus2net
Note that we must use the function session_start() at the starting of the page ( before sending any output to browser ) to use any session variables.
if(isset($_SESSION['userid']) && !empty($_SESSION['userid'])) {
echo " session is available, Welcome $_SESSION[userid] ";
}else{
echo " No Session , Please Login ";
exit;
}
As you can see we have used isset function to check the presence of session variable storing userid. If the variable does not exist then we are displaying a message asking the user to login and then terminating the script by using exit command. This part of the script you can see in detail at our login and logout script.
$_SESSION[userid]
) but we can store other data also. Let us add name to the session variable like this.
$_SESSION['name']="Ronny";
Now we can display all the elements of the session array and see what are the session variables created and what data they store.
while (list ($key, $val) = each ($_SESSION)) {
echo "$key -> $val <br>";
}
you will get a list like this.
userid -> plus2net
name -> Ronny
if (isset($_SESSION['userid']&& !empty($_SESSION['userid']))) {
// Welcome message
}else {
// Ask the user to login
}
session_unset();
session_destroy();
Important : Don't forget to give session_start() command at the staring of the page.
session.gc_maxlifetime = 1440
You can test all the above code to learn and understand how the session works by running these sample codes at your system. Open the index.php file and rest you can easily understand. James Abuda | 23-06-2009 |
The script is very usefull, thanks for that script. actually plus2net site can help me in my thesis.. |
Kanwar | 15-09-2009 |
Frankly admitting, I have been successful after one year of search for a good script like this. |
wahid | 14-01-2010 |
Thats a great website. |
Guru | 19-01-2011 |
Thanks Dude you have done a great job... |
M.elShabrawy | 10-07-2011 |
thanks ,good information session is good for applying web security techniques |
dave | 03-01-2012 |
Very good php scrip; works well for me. Thanks. |
faizan | 03-02-2012 |
thanxxx man u did a great job ... u help a lot.. plz continue this work.... |
malay | 27-02-2012 |
its very productive for developer like me.its really help me doing difficult work in a simple way....... |
Omar | 02-08-2013 |
You did a great job and easy to understand because I am learning PHP....very very helpful....Thanks. |
Gandharv | 13-08-2013 |
I am storing some value in array dynamically & inserting that array in session variable. Once the page get refreshed, session value are refreshing. How to retain the value in session after refreshment? Thanks |