Is there a way to track how the user landed on page x?

Not sure if this should be on a PHP forum…just wondering how it can be done. Basically:

  1. User clicks on a link OR types the url in the address bar: www.mydomain.com/unknown-page

  2. The url redirects to my custom page www.mydomain.com/404

  3. The script emails me that a person landed on the 404 page.

  4. Left to do: Track which link the user came from or clicked on that triggered the 404 error (in this case, www.mydomain.com/unknown-page)

Is number 4 possible, either with PHP or JavaScript or the like?

If the user has it enabled you may find this information within the referrer.

That is the whole purpose of Google Analytics. There are others that use something called a tracking pixel, but that is likely more than you need currently.

https://skillcrush.com/2012/07/19/tracking-pixel/

And if generalizing info will suffice, then i use a session variable to store lastpage data for my error pages. One can’t tell which specific link was clicked but you know where the link resides. Maybe this method is helpul to you.

And i forgot about query strings. Query strings are meaningless until you do something with them (file_get_contents, echo) etc. Handling a query string is dangerous business but you can still use them for information purposes. Either way, a query string can be used on every link in your site.

Thus querylink.php contains

<a id="link1" href="querytest.php?page=querylink&id=link1">querytest</a>

links to querytest.php, which contains:

<?php

//page=querylink&id=link1
$errorlink = urlencode($_SERVER['QUERY_STRING']);
$errorlink2 = urlencode("javascript:alert('OR 1');");
echo $errorlink . "<br />";
echo $errorlink2 . "<br />";

?>

essentially, you can store last page in a session variable, then use the query string to know which link was clicked.

analytics centralize data, which essentially allows a third-party to know info about your site and its users. use with caution.

Sponsor our Newsletter | Privacy Policy | Terms of Service