Session error


#1

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?


#2

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:

somepage.php

session_start();
include 'headers.php';

headers.php
// session_start() not needed here anymore.

Scenario 2:

somepage.php

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

headers.php
session_start();


#3

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?


#4

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).


#5

Although it is currently handled this way:

http://php.net/manual/en/function.session-start.php

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.


#6

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

<?php
// error checking, display errors, etc goes here

if( isset( $_SESSION ) ):
  // session already started
else:
  session_start();
ending;

#7

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


#8

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


#9

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).


#11

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_start();
$_SESSION['foo'] = 'bar';

This will not work

$_SESSION['foo'] = 'bar';
session_start();

#12

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


#13

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