I need to restrict a particular php file from directly getting accessed. Not through htaccess any other method please.
Asked
Active
Viewed 305 times
1
-
1if you don't want it directly accessed, then DON'T PUT IT IN YOUR DOCUMENT ROOT. – Marc B Aug 16 '13 at 17:33
-
So... through no method? Typically you put things like classes in a private folder. – ironcito Aug 16 '13 at 17:33
-
What are you trying to do? Put a webpage up that people can't access via a web browser? – warpedspeed Aug 16 '13 at 17:34
3 Answers
2
I would put a constant inside the root file that loads "childfile". After you try to access the childfile.php directly, it dies on the access error, because that constant is not defined.
Root file:
<?php
define('LOADED', TRUE);
include('childfile.php');
...
childfile.php:
<?php
if( !defined('LOADED') )
die('You cannot access this file directly!');
...

Mario Dian
- 166
- 5
2
Put the PHP file in a directory that is outside of your web root (ie- higher up the structure than htdocs/ or www/ or whatever you're using). You can still require it in PHP, but it can't be accessed by HTTP requests.

XaxD
- 1,429
- 14
- 27
1
A common approach is to define a constant (e.g. 'APPLICATION') in a common file and then, in the file you don't want directly accessed put:
if(!defined('APPLICATION')){ die("Don't look at me!"); }

djheru
- 3,525
- 2
- 20
- 20