views:

122

answers:

1

I have some basic pageview incrementing code set up on a LAMP server (see code below).

The code starts a session, checks if pageviews is set, increments it if not and prints the variable. The page links to itself with a single href, and clicking on that link causes the pageviews count shown on screen to increment by one. It works correctly in IE8, Safari4 and Opera10. But in Firefox3 and Chrome4 it increments by two each time I click the link. I am using a fresh install of both Chrome and Firefox, and I have tried this in Firefox safe mode as well.

Paradoxically, I have the exact same code working correctly on another live site on the same server.

This might be more of a browser question than a PHP question :-)

Thanks in advance for your help!

<?
// start session
session_start();

// increment page views
if (isset($_SESSION['pageviews'])) {
$_SESSION['pageviews'] ++;
}

// if new session, set page views at 1 and capture source URL
else {
$_SESSION['pageviews'] = 1;
$_SESSION['sourceurl'] = $_SERVER['HTTP_REFERER'];
}

// capture user ip address
$ipaddress = $_SERVER['REMOTE_ADDR'];
?>
+1  A: 

Most usual cause is improper human-readable urls implementation, when router acts as a 404 handler, thus, nonexistent favicon.ico would make another request ;)

Col. Shrapnel
Thanks, Colonel - you nailed it! I added a favicon and it works properly in both browsers now. But your answer is a little bit over my head. Are you saying that my Apache server is handling URLs improperly, or that the browsers are?
Adam
@Adam your index.php should distinguish requests and serve only expected ones. Say it should count only / requests and nothing else. All requests patterns must be pre-written in your scriptIf none matched, no cation must be taken and 404 error must be thrown instead
Col. Shrapnel