Is it possible to detect a visitor's browsing history using JavaScript or PHP?

I want to check if my site’s visitors have visited another particular site before coming to mine.

I know how to use JS and PHP to check to see (via referrer info) if the user has just come from that site to mine, but I would like to be able to detect if they have visited this site at any time before (not just immediately before coming to my site). Can this information be detected?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Unfortunately, this is possible this is possible.

You can apply a CSS rule to a:visited that has a background image for a PHP script.

Solution 2

You used to be able to do it (with JavaScript and the CSS psuedo class :visited), but browsers have fixed that exploit.

Solution 3

Yes of course! simply ask the other site if you can pop a script on their site which passes your system the information needed 😉

Solution 4

There is a hack that allows you to do this, but you won’t be able to do it deterministically for all browsers as they may try to hide this information. Also, you will not be able to determine that on the server side, but only through the client side. The idea is to manipulate the stylesheet of visited links as @SLaks already states while I was typing :).

Let’s say if you were interested in finding out if a site user has visited google.com. Then insert a link to google into your page, and set a unique style for visited links, the effects of which are known beforehand. In the case below, visited links will be colored with #012345.

<style>
a:visited {
    color: #012345;
}
</style>

<a href="http://www.google.com" rel="nofollow noreferrer noopener"></a>
<a href="http://www.amazon.com" rel="nofollow noreferrer noopener"></a>

Then go through each link you have inserted into the page, and get it’s color. If it’s #012345 for the above example, then the user has visited that link. You can’t actually access their history to know which sites they visited, however. It’s more of a polling process.

Solution 5

If you don’t mind probabilistic answers, you can time how long it takes to do DNS lookups on the hosts in question. If the DNS answer comes back very quickly, then they might have done a DNS request for that host recently. If the DNS answer comes back slower, then they might not have done a DNS request for that host recently. Of course, my cable modem is going to be giving much faster results than CDMA or GSM phones, so it might only be useful if you’re comparing several sites on a single machine.

Edit, in response to Alex’s point about ISP DNS caching:

For example: With a local DNS cache, my initial queries for domains took an average 1.6 seconds. (I assume because the cache was very cold, and needed to find .com, .co.uk, and .co.jp nameservers.) Hot-cache queries averaged 0.006 seconds. My DNS cache is a recursive resolver, so it does not use my ISP’s caching resolvers.

Without using my local DNS cache and using the DNS cache on my cute router, my initial queries averaged .910 seconds and hot-cache queries averaged .514 seconds. I don’t know if the .4 seconds saved are from my router’s DNS caching or my ISP’s caching. But even .4 seconds should be visible in Javascript.

There is enough data to make some guesses: if the time for the first and second attempts to resolve the domain name are similar, you can assume the cache was hot and the address had been used recently. (Perhaps hot at the ISP level, but this was marketed as a probabilistic method in any event. 🙂 If the times are dissimilar, you can assume the cache was cold.

When using my ISP’s DNS, I guessed nine ‘hot’ domains and nine ‘cold’ domains, and the average lookup time for hot was .226 seconds, and the average lookup time for cold was .308 seconds. The difference of .082 seconds might not be large enough to notice in javascript, and it definitely pales in comparison to the differences between known-hot and known-cold lookups using a local cache or my cute little router for DNS.

Of course ‘hot’ and ‘cold’ are relative to the TTL for each individual domain.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply