Session error

Hey guys…

I have included session_start at the top of each page but am creating a website where each page is linked to the header.php, which also has session, does this mean that i would only need 1 session in header.php?

Sometimes, i have been getting session already started notice and i noted that it is not an error, can i ignore this on a live server?

If you have session_start() on top of every page and you include headers.php to these pages. Then headers.php do not need another session_start(). Other way around if headers.php is included to your every page at the top of the pages you can have session_start() only in headers.php and remove it from the top of the pages.

Scenario 1:


include 'headers.php';

// session_start() not needed here anymore.

Scenario 2:


include 'headers.php';
// Some more code..


1 Like

Thanks and my other question is… will this notice warning appear in a live server? Can i just ignore the notice as it is not really an error?

The difference between a PHP error and notice is that the former also stops script execution. Despite that you should never ignore issues (compare it to the low fuel alert in a car, it will still run … up to a certain point in the future).


Although it is currently handled this way:

As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.

there is no guarantee it will always be so. It doesn’t make sense to me to leave code with a known error in place simply because one can get away with doing it.


I would recommend inserting this script to check if a session has already been started.

// error checking, display errors, etc goes here

if( isset( $_SESSION ) ):
  // session already started

I am trying to log the user out first before signing up, so I guess i would need a session start first before saying…

session_start ();
If (isset ($_session ['u_uid'])) {
   header ("Location: index.php?signup=logoutfirst");
 exit ();
} else {
   header ("Location: signup.php?signup=success");
  exit ();

I guess if i linked to header.php, then i shoukd omit that session_start

I think it’s safer to use session_status() or session_id() to check if a session has already been started.

1 Like

This particular notice at this moment of time won’t break your program and is anyway recommended be dealt with correctly as others have already stated. Will the notice appear on the live server also? That depends on the server/php configuration. Added to that you should never show any notices, warnings or errors to the end user in a live production site. You should log them instead somewhere (most of the time to a logfile).


Please take care that variables in PHP are case sensitive, meaning $_session does not refer to the session variables. For that you must use $_SESSION (all caps).

Also, you must call session_start() before doing anything with $_SESSION

This will work

$_SESSION['foo'] = 'bar';

This will not work

$_SESSION['foo'] = 'bar';

4 posts were split to a new topic: Problems getting PHPmailer working

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.