Using define to protect files in PHP
In my other tutorial I showed you how to include files dynamically using PHP [like main.php?x=whatever] The files that you include can be accessed by anyone, which you may not want done.
Let's look at the code: ############ # Main file: ############ <?php define("MAIN", "True"); ?> # ############################ # Files that will be included: # ############################ <?php if(!defined("MAIN")){ die('<tt>You cannot view this file directly!</tt>'); } ?>
The first part, you put in the file that includes the rest of the files. You can change MAIN and True to whatever yout want, just make sure to change it in the second part too.
Now we have to make the include files check if 'MAIN' (or whatever you changed it to) is defined. We do that by putting the second part of the code at the top of every include file. This will disallow direct viewing of any of the include files.