在PHP中链接Referer Origin

I am trying the following. I have a facebook ad that has redirect link. This link leads to lets say xyz.com. I have have access to xyz.com.

I like to find out from where the user came from to xyz.com.

Basically, I am trying to avoid to let people copy the link from the facebook ad and put on their personal sites. Is that even possible to detect whether through Javascript or PHP if possible.

In PHP, there is the global variable $_SERVER['HTTP_REFERER'] available, but it contains a value provided by the client (the browser when interacting with humans) and cannot be relied on to be consistently set. If there is a redirect in between, possibly you see only the server where the redirect is coming from. JavaScript has the history object, but does not allow you to see its contents for privacy reasons.

If you do not need 100% reliability, I would propose to log the data in $_SERVER['HTTP_REFERER'] for a few days and then have an eye on the records, thus allowing you to spot how the traffic origin is distributed.

Depending on your server's settings, the referer may also be contained in the access log. For Apache, see the documentation here.