Google Adsense causes empty $_SESSION variables?

I have a very simple page, in which I check to see if there is a $_SESSION[‘username’] set, to see if the user is logged in. If not, I issue an error, which I email to myself.

When I display the page for the first time, it appears correctly (I am logged in) BUT I receive an email stating that session info isn’t set. If I put an “echo” statement or something in the “else” of that check, it never appears: page is always displayed correctly!

SO, I searched for days, and found nothing. Finally I just started deleting parts of my page, until I found that if I remove Google Adsense javascript code, the problem goes away!

So it’s as if the adsense code is causing a “render” of the page without session variables set. Then the session variables are set, and the page is displayed correctly!?

Now the “first time” is a hint. Subsequent visits to that page don’t cause errors. If I clear cookies and browser data (I didn’t check to see if it’s cookies specifically, I assume it is) then the error happens again.

Here’s the code on the page causing the error:


include $_SERVER['DOCUMENT_ROOT']."/../db_live_connect.php";

if (isset($_SESSION['username']) && $_SESSION['username'] != "") {
	// (Do stuff here)
} else {	// If our session is not set or seems to be set incorrectly
	// (This shouldn't happen! Page is always displaying correctly!?)

	$error_msg = "Temps steps page failed.\\r\
 Mysql_error: [".mysql_error()."] and GET is: ".$_GET['g'];
	// Define $error_msg prior to including log_error.php
	include ($_SERVER['DOCUMENT_ROOT'].'/include/log_error.php');

}	// End if ( isset($_SESSION['username']) && $_SESSION['username']!="" )

	<title>Dreams: Steps</title>

   <script type="text/javascript"><!--
    google_ad_client = "pub-nnnnnnnnnnn";
    /* Vertical short */
    google_ad_slot = "nnnnnnnnnnn";
    google_ad_width = 120;
    google_ad_height = 240;
    <script type="text/javascript"


Any ideas?? How to solve this? Obviously I can live without the email messages, by just removing them, but I would like to turn that error checking on, and reporting. PLUS, I think it’s a sign of a more serious issue.

ps: I set that session variable very simply:

$_SESSION['username'] = $uRow['username'];
header ("Location: /home/");

Thanks! :confused:

again, it’s impossible for the javascript to mess with your session unless it’s AJAX calling a PHP page on your server.

isset should have returned a boolean… according to your script it’s returning blank.

$error_msg = “Temps steps page failed.\r
Mysql_error: [”.mysql_error()."] and GET is: ".$_GET[‘g’];

I’m assuming you’re getting Temps steps page failed.\r
Mysql_error: and GET is: 2
(when g is 2)

add to this message: isset($_SESSION[‘username’]) and check to see if it’s true or false. If it’s true, something is erasing your variable. If it’s false, your session isnt being preserved.

Tried that (I actually had that in before, so that when a normal error was reported, I could see which user was having the problems.)

Code is now:

$error_msg = "Temps steps page failed: ".isset($_SESSION['username'])." \\r\
 Mysql_error: [".mysql_error()."] and GET is: ".$_GET['g'];

And output was:
error_msg: Temps steps page failed: Mysql_error: [] and GET is: 298

But again, the crazy thing is: the page displays perfectly! Even if I put something like an echo “session not set!”; or a redirect
header(“Location: /help/”); it still displays fine. Doesn’t redirect, or anything. Never echoes error messages. ALL it does is send me the error email. Like I said, it’s as if it’s “pre-parsing” the file before the browser actually views it.

I am completely stumped. :frowning:

Thanks for your interest thus far though! :slight_smile: Any more ideas?

ps: I just did a check. If either of the google Adsense “script” blocks are removed (you’ll see there are two of them) the ads don’t work, but the error doesn’t occur. Both need to be present… Will play around with that some more!?

Javascript cannot create session variables.

Your script as pasted here does not create session variables either. This code should always go to the else clause.

In the login page (index.php, separate from the page that’s causing the error, steps.php) $uRow is being set by a MySQL query for the user’s username. This could happen much earlier in the session, and seems to work fine. The login is on a separate page, that never gets revisited.

So I’m only using “session_start()”; to “re-acquire” the session variable. The entire page is displayed above (the one that causes the errors.) I’m actually not setting $_SESSION[‘username’] again at all, but still I get the error whenever I visit a new page, or even modify the $_GET data with a new value, like steps?g=2 would cause an error (then never again) and I go to steps?g=3, I’d get the error once, etc. Revisiting any of those pages wouldn’t cause an error.

I’ve simplified my page further… removing most includes (copying contents to the page in question), and still nothing. Someone must know! I even checked that it’s happening also in IE … help!

What is setting $uRow?

Here’s what i’m guessing right now, btw:

You log in. $uRow gets set, so $_SESSION[‘username’] gets set to that value.
You revisit the page. $uRow isnt set this time, so $_SESSION[‘username’] gets set to “”. The Else clause triggers.

I’m setting the session variable in the other (login) page, as in that last little code snippet, at the bottom. Not trying to set it via javascript (I’m not even using javascript at the moment [Ed: other than the pasted Google Adsense code]). Please have a re-read. The page IS displaying as if the session variable is set correctly (even echoing that variable) but I’m still seeing the “else” clause get visited… but it doesn’t seem to be happening “on screen”!?

I realize this is an old thread, but I was having a similar issue today and happened upon this thread. And I think I might have found an explanation so I thought I would post it for anyone else who finds this thread.

The javascript isn’t directly interfering with the $_SESSION variable. However, if you look actually look at the Google Ad javascript, it dynamically creates an iframe tag on the page to display the ad. I believe that iframe is what causes the issue:

  1. Your page loads
  2. JavaScript runs and creates the iframe
  3. The content that loads in the iframe unsets the $_SESSION because there’s nothing on Google’s side to preserve it

The only way I can think of to get around this is to use $_COOKIE instead of $_SESSION, which is actually the better option for many reasons. The only reason I’m using $_SESSION is because the code base I work on is ancient and there are some legacy issues that I have to deal with.