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?
I have a file that I need to act differently when it's included vs when it's accessed directly (mainly a
print()
vsreturn()
) Here's some modified code:The file being accessed is always an included file, hence the == 1.
1: Checking the count of included files
Logic: PHP exits if the minimum include count isn't met. Note that prior to PHP5, the base page is not considered an include.
2: Defining and verifying a global constant
Logic: If the constant isn't defined, then the execution didn't start from the base page, and PHP would stop executing.
3: Remote address authorisation
The drawback with this method is isolated execution, unless a session-token provided with the internal request. Verify via the loop-back address in case of a single server configuration, or an address white-list for a multi-server or load-balanced server infrastructure.
4: Token authorisation
Similar to the previous method, one can use GET or POST to pass an authorization token to the include file:
A very messy method, but also perhaps the most secure and versatile at the same time, when used in the right way.
5: Webserver specific configuration
Most servers allow you to assign permissions for individual files or directories. You could place all your includes in such restricted directories, and have the server configured to deny them.
For example in APACHE, the configuration is stored in the
.htaccess
file. Tutorial here.Note however that server-specific configurations are not recommended by me because they are bad for portability across different web-servers. In such cases where the deny-algorithm is complex or the list of denied directories is rather big, it might only make reconfiguration sessions rather gruesome. In the end it's best to handle this in code.
6: Placing includes in a secure directory OUTSIDE the site root
Least preferred because of access limitations in server environments, but a rather powerful method if you have access to the file-system.
Logic:
htdocs
folder as the links would be outside the scope of the website's address system.Please excuse my unorthodox coding conventions. Any feedback is appreciated.
What Joomla! does is defining a Constant in a root file and checking if the same is defined in the included files.
or else
one can keep all files outside the reach of an http request by placing them outside the webroot directory as most frameworks like CodeIgniter recommend.
or even by placing an .htaccess file within the include folder and writing rules, you can prevent direct access.
I wanted to restrict access to the PHP file directly, but also be able to call it via
jQuery $.ajax (XMLHttpRequest)
. Here is what worked for me.