View Full Version : PHP Notice: A session had already been started - ignoring session_start()

07-16-2007, 02:53 PM
I have a permission file I include at the top of each page in my application that I am use to do the session_start() and check for the session.

I am getting an error that shows on each page though that says:

PHP Notice: A session had already been started - ignoring session_start() in C:\DATA\htdocs\includes\permissions\universal.php on line 2

Any help would be greatly appreciated.

The page runs fine, but the error shows.

I tried searching for an answer, but the query in the search box was freaking the site out.

07-16-2007, 03:07 PM
Either you are including the file multiple times on a page; or you are including it on a page that has its' own session_start() statement; or your php.ini (possibly .htaccess) is automatically starting a session before you do - http://www.php.net/manual/en/ref.session.php#ini.session.auto-start

Edit: You can use the following logic to only start a session if it has not already been started -


07-16-2007, 03:18 PM
Thank you that was my next question. It looks like an include for calendar on the page has the session start too. Thank you for your help!

07-16-2007, 04:20 PM
You can also just supress the error message with @

It wont do any harm if it fails, its a notice, its not even a warning, let alone a fatal error. PHP is letting you know, just in case somethings going on that shouldnt be or that you dont know about.

07-16-2007, 06:02 PM
a suppressed error is still an error (and still uses up cycles to handle internally (more so if you log errors)), there are times when an error may be part of the normal scheme of things often with database calls etc which maybe expected to fail from time to time but for general use you should (IMO) try and fix the error, not hide it.

07-16-2007, 06:34 PM
But its not an error. Its a 'notice'. In terms of processing time it takes no longer to check if the session is set than it does to attempt to start the session and supress a notice.
PHP4 & 5 suppress this level of errors by default. If your error reporting hadnt been changed to display notices, you wouldnt need to do either of these things.

Notices can help when debugging, they can help point out typos and crap like not quoting array keys when you mean them to be strings not constants - stuff you should fix if you can but if your script works (and you understand the notices and what they mean) theres nothing wrong with supressing them.
In a production environment, its normal for all errors to be supressed.

07-17-2007, 02:50 AM
In a production environment, its normal for all errors to be supressed.

correct, but normal testing environment would work with E_ALL and remove as many notices or warnings as possible by fixing the error/notice/warning ,not by hiding them.

In the example of this thread we have a session getting started from ...somewhere... surely its best to track that down and find out what else that page is doing that it should not be ? the end result is probably harmless here, but when you start debugging you need as much info as you can get and not have it hidden.

My advice (now, since I used to be of the same opinion as yourself) is to turn on E_ALL whilst testing and only suppress with '@' when you know why you are doing so and when the alternative is not desirable.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum