Do you have PHP files that you do not want visitors to directly run but would still like to use them in your code? Here is a little trick picked up from the code in the phpBB forum code.
Create a file named config.php or some other file that you are going to include before any other code. In this file put the following:
define('INMYSCRIPT', 1);
This code creates a defined variable INMYSCRIPT which can be named as anything that you want. Then on every other page in your script that you do not want to be ran on their own put the following: (This filename is code.php)
if(!defined('INMYSCRIPT')){ echo 'You cannot access this file directly.'; die; } // Whatever other code you wish goes below here echo 'This file is safe to be ran';
Now, instead of the file outputting any data to the browser that could be harmful, the only content output is: “You cannot access this file directly”. You could very easily create a re-direct script to transfer them to a specific page or you could modify the text to any other HTML or other content you wanted.
Open up your web browser and try accessing code.php. You should get that error message that we created. Now if you create one last file that we will name as test.php, place the following:
require('config.php'); require('code.php');
Now if you try accessing the test.php file from your web server you will be shown: “This file is safe to be ran”.



