184

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?

2
  • 13
    instead of the die() you should test 'header("HTTP/1.1 404 File Not Found", 404); exit;'. This will (at least on apache) make the server return the normal 404 page. Commented Jan 4, 2009 at 17:08
  • 1
    Here are two easy methods I have explain to disable direct access in PHP included files - codespeedy.com/disable-direct-access-to-the-php-include-file Commented Jul 18, 2017 at 21:06

33 Answers 33

1
2
-1
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

will do the job smooth

Sign up to request clarification or add additional context in comments.

2 Comments

Copy paste from CodeIgnitor. That's cool but it actually doesn't do anything on it's own. The BASEPATH const is set in a index.php file that lays at the bottom of the tree structure. CI rewrites the URLs so there is no need for accessing the scripts directly anyways.
i know there is no need but if any one tries to do so
-1

Redirect from that file to some other page. (like index.html)

.htaccess:

Redirect 301 LINK_TO_YOUR_PHP LINK_TO_INDEX.HTML 

Comments

-7

You can also try renaming the document you don't want people to be able to access. You could rename it to 47d8498d3w.php for instance. Just make something up that people most likely won't type as a http-request. If you include the file with SSI or PHP, the user won't be able to see the name of the document anyway.

2 Comments

You should never use that method to secure your files from unwanted executions.
Security by obscurity is not the best answer in this case :)
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.