lisovaccaro
lisovaccaro

Reputation: 33956

Does using `session_start()` when session already started cause any problems?

I have many occurences of session_start(); in my site. Due that some files that are included are also called with AJAX and need to start session in this cases.

When I wrote the site I didn't care about starting it more than one time since I knew my server simply ignored it when session was already started.

Is there any problem with doing this? Should I check beforehand if session is started before calling it again?

Upvotes: 1

Views: 194

Answers (1)

aioobe
aioobe

Reputation: 420951

Is there any problem with doing this?

It will result in an E_NOTICE, but other than that the second (and third, fourth, ...) call to session_start has no effect.

However, personally I consider it a code smell if you "don't know" if you've started the session already. If possible, try to design the code so that it is evident whether or not the session has been started already.

Also, as James points out in the comments in the manual:

To avoid the notice commited by PHP since 4.3.3 when you start a session twice, check session_id() first:

if (session_id() == "")
    session_start();

Upvotes: 1

Related Questions