Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

random seeming PHP $_SESSION errors

5:38 pm on Apr 14, 2006 (gmt 0)

5+ Year Member

Hey all,

I am am having a completely baffling intermittent problem with a login page that sets a session up.

It seems totally random whether the session is set or not. Sometimes it works and sometimes it doesn't.

On all subsequent pages I have included the 'session start();' command (am I missing something there? should there be something in the brackets?) via an included auth page that checks to see if a couple of the elements are set. If not it chucks you out to the login page. The same test is done by this same file on every page.

So maybe there is a problem with the login file itself:
session_start ();
if (isset ($_GET['do'])) {

$sql = "SELECT staff_username FROM staff
WHERE staff_username='$_POST[staff_username]'";
$result= mysql_query($sql)
or die("Could not execute query.");

$num = mysql_num_rows($result);
if ($num >= 1)
// login name was found
$sql="SELECT staff_id, staff_username, staff_adminlevel FROM staff
WHERE staff_username='$_POST[staff_username]'
AND staff_password='$_POST[staff_password]'";
$result2 = mysql_query($sql)
or die("Couldn’t execute query.");
$num2 = mysql_num_rows($result2);
$row = mysql_fetch_assoc($result2);
if ($num2 > 0) //password is correct
$logid= $row['staff_id'];
$adminlevel= $row ['staff_adminlevel'];
$datetime= date("Y-m-d h:i:s");
$sql = "INSERT INTO staff_logintime (staff_id, staff_logintime) VALUES ('$logid', '$datetime')";
$result= mysql_query($sql) or die ("cant add the date time stuff");
header("Location: admin_area.php");
else // password is not correct
$message="!The login name, '$_POST[staff_username]'
exists, but the password is incorrect!<br>";
elseif ($num == 0) // login name not found
$message = "The login name you entered does not
exist! Please try again.<br>";
else {

I am new to this so I am sure there are some screwups in there but why does it work some times <i>with the same pages</i> and coming from the same locations, and not at others!?

Is it a problem with my web host maybe? Do I need to make the pages go from one to another more slowly or something? do I need to do something to retrieve the session variables and turn them into page variables? I have tried everything I can think of for days and just not cracked it.

Hope someone can help


6:04 pm on Apr 14, 2006 (gmt 0)

10+ Year Member

are you using session_destroy() when you are testing?
7:54 pm on Apr 14, 2006 (gmt 0)

10+ Year Member

You should check to make sure you are not outputting any whitespace before you call session_start() on your pages.

This could cause the symptoms you are describing.

5:43 am on Apr 15, 2006 (gmt 0)

10+ Year Member

are you using subdomains?
if so, yo need to do this on the scripts in the subdomain:
ini_set("session.cookie_domain"," .domain.com");
7:29 am on Apr 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

How about starting with your double curly brackets I noticed when it is supposed to be just one. }}


10:48 am on Apr 15, 2006 (gmt 0)

5+ Year Member

Firstly let me say how grateful I am to all those who have replied here for taking the time and consideration to reply- You guys really are the unsung heroes of the webmaking world and are totally integral to the success or failure of countless ventures across the globe these days. You all know that I am sure, but if you need any help I can give drop me a line- I am very good with photoshop, avid editing, Premiere, After Effects, stuff like that.

Anyway it seems like the white space could have been the problem. I have deleted it all (being an arty type for whom none of this comes n aturally in the slightest my pages were littered with white space to help me make sense of it all!) Thus far on brief testing it is looking promising.

If any of you know and have the time to explain I would be very interested why 'nothing' (i.e. white space) can cause a promlem in the computer world?

Anyway thanks very much for you time and help BUT I may be back!

1:17 pm on Apr 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

session_start () doesn't allow you to have any outputs before it runs (at least I faced so). White space is also seen as an output.

I am sure some one can give you more detailed response on this.


3:08 pm on Apr 15, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

it is only "nothing" outside of your php tags

before <?php or after?>

that counts as output and causes headers to be sent to your browser.

A blank line is not nothing, it consists of a linefeed or carriage return, or both.

1:24 pm on Apr 16, 2006 (gmt 0)

5+ Year Member

I am afraid after an initial euphoria the problem has not gone away. It is simply that random. There is now no white space anywhere on any pages.

My admin_auth.inc.php file is like this:

session_start ();
if ($_SESSION['auth'] ==''¦¦$_SESSION['password'] =='')
$redirect = $_SERVER['PHP_SELF'];
header ("Refresh: 2;URL=index.php?redirect=$redirect");
$notlogged= "!YOU ARE CURRENTLY NOT LOGGED IN!<br> we are redirecting you, hang on...<br>(if your browser doesnt redirect you in 10 seconds <a href=\'admin_login.php?redirect=$redirect\'>click here<a/>)";
$logout="<a href='index.php'>log-out</a>";

is there anything wrong with that? I even see that the details that this code looks for is in the session array after I get tracked back sometimes! (via a print_r use for testing). It is just totally baffling. I'll set someone up a login and you can see for yourselfs if it helps?

1:39 pm on Apr 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

You are including this page. What about the page including the file, doesn't it contain white space outputs?


1:43 pm on Apr 16, 2006 (gmt 0)

5+ Year Member

looking at it a bit more today the problem seems to be that the pages are randomly unable to retrieve the session data from wherever it is. Sometimes I can wander around the site with no problem, sometimes I get the you are not logged in error but if I click back (having disabled the redirect line for testing) and try the exact page again sometimes it retrieves the info, sometimes not. Sometimes when I click back the session array is empty sometimes not. I am guessing this must be a server problem, is it common? and what do I have to ask the hosts to do to fix it if it is their fault?
1:47 pm on Apr 16, 2006 (gmt 0)

5+ Year Member

pressing refresh on the page also retrieves the session array properly around 60% of the time...

Featured Threads

Hot Threads This Week

Hot Threads This Month