/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] HTTP_REFERRER – something more reliable?

Hi,

I have some links to download images from my site, and I don’t want any other site linking directly to those image download links other than from a page on my site.

So I wrote something like this:

[code=php]<?php

$referrer = $_SERVER[‘HTTP_REFERER’];

// CLEAN URL FACTOR
$url2 = strip_tags($referrer);
$url2_array = explode(“/”, $url2);
array_shift($url2_array); // First one is empty

if($url2_array[1] != “mydomain.com”) {
echo ”
<script language=”JavaScript”>
<!–
window.top.location.href=”http://mydomain.com”
–>
</script>
“;
}
?>[/code]

The script works fine, but the problem is that I’ve heard HTTP_REFERER is not all that reliable – that there will be some browsers that do not support it, and thus the variable $referrer will turn up null even when they were referred to the download page from a page on my site.
Is there a way around this?

Thanks for your time!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 23.2009 — Best I can think of right now would be to use a random token in the link, and keep a copy in the session data. You could then use that as a fall-back if the HTTP_REFERER header is empty, checking to see if that token is present.
[code=php]
<?php
session_start()
$token = uniquid();
?>
<a href="/page2.php?token=<?php echo $token; ?>">Click Me</a>
[/code]

Then in the second page, if the HTTP_REFERER is not present, check to see if both $_GET['token'] and $_SESSION['token'] are not empty and have the same value. You might also save the current time() in another $_SESSION variable to verify that it's not too old.
Copy linkTweet thisAlerts:
@criterion9Oct 23.2009 — I also would say you should use header redirection instead of relying on javascript in your example. It would be easy to get around the javascript redirect by disabling javascript.
Copy linkTweet thisAlerts:
@boxofmochiauthorOct 24.2009 — Thank you so much! That idea worked like a charm NogDog!

And you're right about the Javascript criterion9 - I've fixed that too ?.

Thanks again!
×

Success!

Help @boxofmochi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...