3

Hi I was wondering how when a user comes to one of my web pages, say "page.html" I would like to check if the last page they visited before they came to "page.html" was "google.com" (or any other website). Is there a way to do this or must I have control of the the other web page that they are coming from in order to monitor this? I assume this can be done in php if it is possible.

Twisterz
  • 424
  • 4
  • 15
  • 31
  • Possible duplicate of [How do you access browser history?](http://stackoverflow.com/questions/48805/how-do-you-access-browser-history) – Alex Kalicki Aug 16 '12 at 15:48
  • He wanted to access browser history, I just wanted to know how to monitor how a user comes to my website, different question. – Twisterz Aug 16 '12 at 16:02

3 Answers3

5

You're looking for the HTTP Referer value, which in PHP you get using $_SERVER['HTTP_REFERER']. Note that this is an entirely arbitrary value volunteered by the client's browser, so it's not guaranteed to be correct or trustable.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    Also worth noting... Clients SHOULD NOT include a Referer header field in a (non-secure) HTTP request if the referring page was transferred with a secure protocol. So HTTP_REFERER will be empty if user comes from HTTPS site (e.g. Google), to your HTTP (non secure) site. – Dejan Marjanović Aug 16 '12 at 15:51
  • So this value is not very dependable. That is somewhat of an issue, because I need it to be. Any ideas on how I could make it more dependable, or use a different function that is? – Twisterz Aug 16 '12 at 15:52
  • @Twisterz If you do not control the originating server where you could make it hand over some token or cookie or something else... No. – deceze Aug 16 '12 at 15:54
2

Use PHPs $_SERVER variable to get the referrer to your page. http://php.net/manual/en/reserved.variables.server.php

$_SERVER['HTTP_REFERER'];
WhoaItsAFactorial
  • 3,538
  • 4
  • 28
  • 45
1

Have a look at the $_SERVER documentation you want $_SERVER['HTTP_REFERER'] but take note that it's not always going to be set, it depends on the user agent.

piddl0r
  • 2,431
  • 2
  • 23
  • 35
  • Is there anyway to guarantee it being set, or using a different function that is more dependable? – Twisterz Aug 16 '12 at 15:49
  • 1
    this question has an accepted answer that will help understand when it won't be set : http://stackoverflow.com/questions/6880659/in-what-cases-will-http-referer-be-empty – piddl0r Aug 16 '12 at 16:01