Page 1 of 1
Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 3:36 pm
by JellyFish
Like the title says, are there no consequences to calling session_start more than once? If so, what are the consequences?
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 3:46 pm
by Darhazer
As of PHP 4.3.3, calling session_start() while the session has already been started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.
But why to call it twise?
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 3:57 pm
by JellyFish
The reason is because I have this header file that is included in almost every page throughout my site. I put session_start() in that file so that I don't have to keep writing session_start(). Sometimes though, I need the session before I include the header file. So I need to know if it could causes any noticeable problems if I call session_start more then once.
EDIT: Just in case though, how could I test if session_start has already been called?
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 4:42 pm
by Darhazer
Call session_id(), it will return empty string if session is not started
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 4:57 pm
by jackpf
Or
Code: Select all
if(isset($_SESSION))
//session has started
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 5:22 pm
by Darhazer
It's not safe check... you can initialize the $_SESSION variable without starting a session.
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 5:30 pm
by jackpf
You're not supposed to though. It's a superglobal.
But fair point.
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 9:03 pm
by JellyFish
Thanks for the reply guess. In case I ever need to check if a session has been started, I'll user the session_id function. But I see no need to do so sense calling session_start twice doesn't harm anything.
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 9:11 pm
by jackpf
PHP will generate a warning though.
Re: Is it ok to call session_start twice?
Posted: Thu Aug 27, 2009 11:24 pm
by susrisha
here is what the manual says
session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie.