Login |  Register 
I've learnt so much after subscribing to read your exclusive articles!
Nick
 

Sessions

If you want to store information temporarily between pages and refreshes then sessions is a good way to do it. This stores data for a particular user at the server. In order to use sessions you must first call the start_session function.
start_session();
You can then assign values to the $_SESSION global array like so.
$username = "tom";
$_SESSION["username"] = $username;
The name username is the index of the session value and can be retrieved like a normal associative array value, so you can access the values like so...
echo $_SESSION["username"];
You can delete a session value with the unset function...
unset($_SESSION["username"]);
While session_destroy destroys all data registered to a session.
session_destroy();

How to find out if you have started a session

The function session_id will return the string id of the current session, or if there is no session it will return an empty string, so the following can detect if you currently have a session active.
if (empty(session_id())
{
 echo "You have no session";
}
If you start a session twice you can get a notice saying that a session has already started, so to get around this you can do something like...
if (empty(session_id())
{
 start_session();
}

Comments to date: 21. Page 1 of 3. Average Rating:
xzczxc   12:30am on Wednesday, June 27th, 2018 
xzczxczxczxcc
Shobhit Shukla   11:35pm on Monday, March 27th, 2017 
Shobhit Shukla
Anonymous   10:50pm on Wednesday, February 8th, 2017 
start_session();

This syntax is wrong

session_start is correct..
sess   12:58am on Thursday, February 2nd, 2017 
sdfdsf
Cornelia   1:50pm on Wednesday, January 27th, 2016 
We've ariverd at the end of the line and I have what I need!
Rasmus Lerdorf   5:43am on Thursday, January 21st, 2016 
This is Rasmus Lerdorf and I will have this site shut down because this is a piece of crap.
asdadsqwr12rqwdr   2:10am on Tuesday, November 10th, 2015 
thnrj456njdfb93ity094tg
Aditya123   11:52pm on Monday, September 28th, 2015
method name is session_start().
correct please..

ttr   10:50pm on Tuesday, April 21st, 2015 
trtr
lalit lakahra   10:15pm on Friday, January 2nd, 2015 
;;;;;;;;


Page: -1-   2    3   

security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38