Is it ok to call session_start twice?
Moderator: General Moderators
Is it ok to call session_start twice?
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?
But why to call it twise?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.
Re: Is it ok to call session_start twice?
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?
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?
Call session_id(), it will return empty string if session is not started
Re: Is it ok to call session_start twice?
Or
Code: Select all
if(isset($_SESSION))
//session has startedRe: Is it ok to call session_start twice?
It's not safe check... you can initialize the $_SESSION variable without starting a session.
Re: Is it ok to call session_start twice?
You're not supposed to though. It's a superglobal.
But fair point.
But fair point.
Re: Is it ok to call session_start twice?
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?
PHP will generate a warning though.
Re: Is it ok to call session_start twice?
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.