1

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

3
  • 1
    if you don't want it directly accessed, then DON'T PUT IT IN YOUR DOCUMENT ROOT. Commented Aug 16, 2013 at 17:33
  • So... through no method? Typically you put things like classes in a private folder. Commented Aug 16, 2013 at 17:33
  • What are you trying to do? Put a webpage up that people can't access via a web browser? Commented Aug 16, 2013 at 17:34

3 Answers 3

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!'); ... 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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!"); }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.