I have a php file which I will be using as exclusively as an include. Therefore I would like to throw an error instead of executing it when it's accessed directly by typing in the URL instead of being included.
Basically I need to do a check as follows in the php file:
if ( $REQUEST_URL == $URL_OF_CURRENT_PAGE ) die ("Direct access not premitted");
Is there an easy way to do this?
Add this to the page that you want to only be included
then on the pages that include it add
I didn't find the suggestions with .htaccess so good because it may block other content in that folder which you might want to allow user to access to, this is my solution:
I had this problem once, solved with:
but the ideal solution is to place the file outside of the web-server document root, as mentioned in another anwser.
My answer is somewhat different in approach but includes many of the answers provided here. I would recommend a multipronged approach:
defined('_SOMECONSTANT') or die('Hackers! Be gone!');
HOWEVER the
defined or die
approach has a number of failings. Firstly, it is a real pain in the assumptions to test and debug with. Secondly, it involves horrifyingly, mind-numbingly boring refactoring if you change your mind. "Find and replace!" you say. Yes, but how sure are you that it is written exactly the same everywhere, hmmm? Now multiply that with thousands of files... o.OAnd then there's .htaccess. What happens if your code is distributed onto sites where the administrator is not so scrupulous? If you rely only on .htaccess to secure your files you're also going to need a) a backup, b) a box of tissues to dry your tears, c) a fire extinguisher to put out the flames in all the hatemail from people using your code.
So I know the question asks for the "easiest", but I think what this calls for is more "defensive coding".
What I suggest is:
require('ifyoulieyougonnadie.php');
(notinclude()
and as a replacement fordefined or die
)In
ifyoulieyougonnadie.php
, do some logic stuff - check for different constants, calling script, localhost testing and such - and then implement yourdie(), throw new Exception, 403
, etc.I am creating my own framework with two possible entry points - the main index.php (Joomla framework) and ajaxrouter.php (my framework) - so depending on the point of entry, I check for different things. If the request to
ifyoulieyougonnadie.php
doesn't come from one of those two files, I know shenanigans are being undertaken!But what if I add a new entry point? No worries. I just change
ifyoulieyougonnadie.php
and I'm sorted, plus no 'find and replace'. Hooray!What if I decided to move some of my scripts to do a different framework that doesn't have the same constants
defined()
? ... Hooray! ^_^I found this strategy makes development a lot more fun and a lot less:
i suggest that don't use of
$_SERVER
for security reasons .You can use a variable like
$root=true;
in first file that included another one.and use
isset($root)
in begin of second file that be included.You'd better build application with one entrance point, i.e. all files should be reached from index.php
Place this in index.php
This check should run in each linked file (via require or include)