I have a short script that tracks user visits by using cookies. I want to record a new session for each 30 minutes of usage on my site.

It works great WITHOUT Google's Adsense javascript code on the page. With the Adsense javascript in the page, here is what happens:

The user visits the page, the cookie is set, the database is incremented by 1 as normal. Now, when a user requests another page, the database increments by 2 more. Also, this only happens with a page that has not yet been accessed (if they navigate back to that already visited page it does not increment). Also like I said above, this problem does not occur if I remove the Adsense code. So it's like the Adsense code is making the page reload twice, while making my php script not see the cookie each time it reloads.

Any ideas?

Here is the visit tracking code(which is included in each page):

-----begin snip----------------------------------------------------------
if ($_COOKIE["gss"]!=1){
//echo "cookie not set - creating cookie...";

setcookie("gss",1,time()+1800,"/",".gigscout.com");

//update session table
$now = date("Y-m-d");
$arrToday=split("-",$now);
$today_y = $arrToday[0];
$today_m = $arrToday[1];
$today_d = $arrToday[2];

$query = "SELECT Sessions FROM tblSiteSessions WHERE Day='$today_d' AND Month='$today_m' AND Year='$today_y'";
$result = mysql_query($query);
if (mysql_num_rows($result)==0){
//add
$query="INSERT INTO tblSiteSessions (Day, Month, Year, Sessions) VALUES ('$today_d','$today_m','$today_y','1')";
$result = mysql_query($query);
} else {
//update
$row=mysql_fetch_row($result);
$sessions=$row[0];
$sessions++;
$query="UPDATE tblSiteSessions SET Sessions='$sessions' WHERE Day='$today_d' AND Month='$today_m' AND Year='$today_y'";
$result = mysql_query($query);
}

} else {
echo "cookie already set";
}
---------end snip-------------------------------------------

    Hi,

    It's probably because the adsense code needs to reference the page to check the content, and you're not going to be setting any cookies on the google bot. I could be wrong there, but since it's an adsense problem it's probably related to that.

    As for your code, here's a quick suggestion to make things a little cleaner.

    Change:

    //update
    $row=mysql_fetch_row($result);
    $sessions=$row[0];
    $sessions++;
    $query="UPDATE tblSiteSessions SET Sessions='$sessions' WHERE Day='$today_d' AND Month='$today_m' AND Year='$today_y'";
    $result = mysql_query($query);
    

    To:

    //update
    $query="UPDATE tblSiteSessions SET Sessions=Sessions+1 WHERE Day='$today_d' AND Month='$today_m' AND Year='$today_y'";
    $result = mysql_query($query);
    
      Write a Reply...