1

I need to restrict a particular php file from directly getting accessed. Not through htaccess any other method please.

thomas
  • 155
  • 1
  • 1
  • 6

3 Answers3

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