0

I've spent the last 8 hours trying to crack this one.

I'm logging the URL's which users view in $_SESSION array (on wordpress)

from my history.php which I'm including on all pages:

session_start();

$currentpageurl= esc_url($_SERVER['REQUEST_URI']);

is_array($_SESSION["pageurl"]) or $_SESSION["pageurl"] = array();

$_SESSION['pageurl'][] = $currentpageurl;

Then I'm calling the array items to see the log like so:

foreach($_SESSION['pageurl'] as $key=>$value)
    {
    echo 'Page view '.$key.' was '."'".$value."'".' <br />';
    }

That all works. But in the results, the post before the one I visit logs as a 'visited page' as well. I checked all of my files for broken links that may cause http requests, and everything checks out. I even changed Request URI to other things from $_SERVER like SCRIPT URI and still the post before always loads.

If I visit these pages:

mysite.com/article-y
mysite.com/article-z

The array shows the page before (which was not visited):

mysite.com/article-y 
mysite.com/article-x <--not visited, but somehow in session array
mysite.com/article-z
Tom Eberhardt-Smith
  • 110
  • 1
  • 1
  • 13

1 Answers1

1

As you observe different values of the $_SERVER['REQUEST_URI'] variable it is almost certain that everytime you view a specific page another request to retrieve the previous post's page is sent. There could be various reasons for this to happen like scripts and iframes on your page. However from what you describe it sounds likely that link prefetching is the reason for what you observe.

You can easily check that by disabling link prefetching in your browser (see here for Firefox for example).

You might have <link rel='prev' ..., <link rel='next' ... and/or <link rel='prefetch' ... tags in your website's source which encourage the browser to prefetch specific pages. However removing these does not mean that the browser will not try to prefetch anything.

It is not possible to reliably check if a request is a prefetch request solely on the server side. Some browsers send an additional HTTP request header when loading a prefetch, but you cannot rely on this.

One way to solve this is to embed a javascript in your website which logs whenever a page is rendered (e.g. the script is executed) or viewed (utilizing the Visibility API) by sending an asynchronous request to some script on your server.

You can find further information in this answer and the comments on it.

Community
  • 1
  • 1
x-ray
  • 3,279
  • 5
  • 24
  • 37