Is it ok to call session_start twice?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Is it ok to call session_start twice?

Post by JellyFish »

Like the title says, are there no consequences to calling session_start more than once? If so, what are the consequences?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Is it ok to call session_start twice?

Post 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?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Is it ok to call session_start twice?

Post 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?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Is it ok to call session_start twice?

Post by Darhazer »

Call session_id(), it will return empty string if session is not started
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is it ok to call session_start twice?

Post by jackpf »

Or

Code: Select all

if(isset($_SESSION))
//session has started
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Is it ok to call session_start twice?

Post by Darhazer »

It's not safe check... you can initialize the $_SESSION variable without starting a session.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is it ok to call session_start twice?

Post by jackpf »

You're not supposed to though. It's a superglobal.

But fair point.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Is it ok to call session_start twice?

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Is it ok to call session_start twice?

Post by jackpf »

PHP will generate a warning though.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Is it ok to call session_start twice?

Post 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.
Post Reply