Forum Moderators: coopster

Message Too Old, No Replies

is it safe to call session start() twice?

         

charlee

8:21 pm on May 23, 2008 (gmt 0)

10+ Year Member



Im having problems with a login-system im creating and the session vars i need to keep along the pages. Id like to know if calling session_start() more than once might be a problem in some specific conditions or if it doesnt make any difference anyways. Because i call session_start() every time the user clicks to browse the site since this statement is in a header file that all my pages include.

thanks in advance!

Receptional Andy

8:31 pm on May 23, 2008 (gmt 0)



According to the PHP manual for session_start [uk2.php.net]:

As of now, 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.

charlee

8:45 pm on May 23, 2008 (gmt 0)

10+ Year Member



So it will be ignored..... does that mean I dont have to care about putting a code to prevent start my session twice or I should of?

Receptional Andy

8:49 pm on May 23, 2008 (gmt 0)



does that mean I dont have to care about putting a code to prevent start my session twice or I should of?

It means that there are problems with the way you have coded your login system. I hope this doesn't sound harsh, but I would be very wary of implementing a login system that triggered these kinds of log entries.

rob7591

9:08 pm on May 23, 2008 (gmt 0)

10+ Year Member



If you already have the header included on all of your pages, why would you need to call it again?

If you need to use a session variable before the header is included, I suggest you take the session_start() out of your header and add it into a different include page that you put at the very top of all of your .php pages.

If it's really an issue, your users may see a warning, so you would probably have to change your settings in PHP to disable warnings.

charlee

9:38 pm on May 23, 2008 (gmt 0)

10+ Year Member



Receptional Andy: Your answer is not better than my question. but thanks anyways.

rob7591: Thanks for the help, I guess i will just use the session_start when i just need it but calling a function instead, so this function will have the code to prevent the page to start a session if its running already. Not sure how good this can be but according to what Receptional Andy said in his first post and those warnings you are mentioning I think it will be the right way. Anyway the way im changing the code right now Im almost sure i never call session_start more than once but just in case :P

Receptional Andy

9:49 pm on May 23, 2008 (gmt 0)



Your answer is not better than my question. but thanks anyways.

Charmed, I'm sure. charlee: the point I was making was that ignoring E_NOTICE will be at your peril - personally, I would not implement a login system without understanding the consequences of this error.

willybfriendly

10:03 pm on May 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not just use require_once() or include_once() which will insure it is only loaded a single time.