View Full Version : PHP Referrer
I'm looking for a script (ideally in PHP) that I can embed within my website that will export the referring domain URL, particuarly when someone clicks on a link to my site from another site or does a search query. I want this variable to be stored in a session and so when the user completes a form or specific operation, the referring page hyperlink gets output with the message.
I already have the form set up but just need to know if there's a better way than doing it with Javascript which I have tried but it seems to be quite unreliable. I had a PHP one working but unfortunately it was only generating the URL of the last page that the user was on, as opposed to the last domain/URL.
Any help is greatly appreciated.
Thanks.
You can access the Referer value in PHP by using $_SERVER['HTTP_REFERER'] (yes, that is how it's spelled).
You can use parse_url (http://php.net/parse_url)() to separate out the hostname and see if it's you own or not (save it to the session if it's not yours; ignore it otherwise). Be aware that a) not all browsers send the Referer field, b) some ISPs or proxy connections strip it out and/or alter it, and c) it is trivial for the user to alter it. You must accept that the value it gives you may be wrong, forged, or missing entirely, and write your script accordingly.
jscheuer1
10-18-2012, 02:23 AM
It's pretty simple but no more reliable than javascript. The PHP referrer and the javascript one both ask the browser for the same data. Not all browsers report the referrer though (it's configurable in some, with others it's just a question of whether they do that or not), and of those that do, one isn't always available. Like if you paste a location into the address bar, the page you land on will have no referrer property in any browser. And I believe it can be spoofed.
But, such as it is, and as I say, the referrer can be gotten via javascript or PHP. It could be something else, but it sounds to me as though you were querying the PHP referrer too often and/or at the wrong time. If so of course you would end up with the latest, which would be if anything a page from your own site in most cases.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.