Forum Moderators: coopster

Message Too Old, No Replies

distinguishing page requests from address bar ones

         

sssweb

8:31 pm on Jun 29, 2010 (gmt 0)

10+ Year Member



Is there a simple PHP way to distinguish between file requests that come through an internal page on my site vs. ones typed into the address bar by the user? I know you can distinguish variables in this way using $_GET and $_POST, but can you do the same for a file (specifically, an image file -- .gif, .jpg, or .png)?

I want to know whether my page requested the image, or a user directly requested it via the address bar.

rocknbil

1:07 am on Jun 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



**Most of the time** a link of any kind will prompt the browser to send a referrer, found in $_SERVER['HTTP_REFERER']. You just need to evaluate it for your domain.


<?php
header("content-type:text/html");
$mysite = 'example.com';
//
if (isset($_SERVER['HTTP_REFERER'])) {
echo "<p>Someone clicked a link</p>";
if (preg_match("/$mysite/i",$_SERVER['HTTP_REFERER'])) {
echo "<p>Linked from my server</p>";
}
else { echo "<p>Coolio, someone off-site linked to me.</p>"; }
}
else { echo "<p>Direct Request, hey, a visit is a visit.</p>"; }
echo "<p><a href=\"https://www.example.com/referer.php\">Test it</a></p>";
?>


Change [example.com...] to your domain, of course . . . .

I say **most of the time** because browser can be configured to not send referrer headers, and some don't, but for the most part it's fairly reliable.

<RANT> (I don't know WHY the environment variable has one "R" in the middle and the proper spelling is with two, REFERRER. Probably the same reason Perl supports "esle" for "else" . . .)

sssweb

3:39 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



Thanks RNB, that works great (like the echoes too)!

sssweb

3:41 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



[oops, that double-posted]

coopster

12:52 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<RANT> (I don't know WHY the environment variable has one "R" in the middle and the proper spelling is with two, REFERRER. Probably the same reason Perl supports "esle" for "else" . . .)


The misspelling originated in the original spec/standard and carried on through since. Even the Apache docs [httpd.apache.org] make note :)

Note: spelling of 'referer' and 'referal' is intentional.


REFERER origin [en.wikipedia.org]