Helping ordinary people create extraordinary websites!

Using Define to Protect Files

By Joe S
2005-08-16

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.




Tutorial pages:
  • Using define to protect files in PHP


© 2005 Joey Shamah


 1 Votes

You might also want to check these out:


Leave a Comment on "Using Define to Protect Files"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS