Get last page visited

Ricardo Neves picture Ricardo Neves · Jul 5, 2012 · Viewed 29.9k times · Source

I need to know if a person that comes to my website went from an another specific website. Example:

  • User A visits www.youtube.com/myvideo and clicks on a link to my website.
  • User B visits google, search my website and click the link.

Results Message on my page:

  • User A: Welcome! You already know how it works, register now!
  • User B: Welcome! Please watch our video first at www.youtube.com/myvideo

My question is:

  1. Is it possible to know the last url the user visited before entering my page?
    I already tried $_SERVER['HTTP_REFERER'] with PHP, but it's not working correctly and I read that does not work on all browsers.
  2. Is there any language or php script to get this url, working on all browser correctly?

Answer

Brad picture Brad · Jul 5, 2012

The HTTP referer header is the only way. This is the data that is given to you in PHP via $_SERVER['HTTP_REFERER'].

Note that this header will work in most cases. Also note that it can be easily spoofed.