I'm about to create a new website and am worried about potential security flaws. I'm focusing on the possibility of others using my PHP scripts (which, as far as I know, is possible with methods such as CORS
or cURL
-- am I wrong?).
I just searched around and found that you could use PHP's $_SERVER["HTTP_REFERER"]
to check the URL from which it was sent.
There were a lot of questions out there on SO, such as this one and this one regarding the use and security of $_SERVER["HTTP_REFERER"]
.
Many of the answers are similar to this one (shown below) that say that it's not safe because software often strips it out:
It may be safe, but it is not reliable: due to the HTTP spec, HTTP_REFERER is optional (some clients don't send this header at all, and some "security" software strips this out from any HTTP request), and there are numerous ways to modify this header. Some browsers send the referring page, some send a blank string, some don't send this at all, some may send bogus data, some may send Aunt Matilda; and moreover, you can't tell whether you're getting valid data in this header or not.
So, no, I would never trust that HTTP_REFERER contains the previous page, and neither should you.
But they just mostly imply that the referer information may just be left out / missing / incomplete, but not wrong.
Therefore, would it be safe to use it like so:
<?php
if($_SERVER["HTTP_REFERER"] != "my_http_referer.php") {
die("You cannot access this script.");
}
?>
Wouldn't this be safe, even if the $_SERVER["HTTP_REFERER"]
is optional and may be incomplete? If it's any script other than my own that's calling this, shouldn't it die()
? Or am I overlooking something?