Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Using Define to Protect Files (0 Comments)
Admin: Posted Date: April 4, 2010

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.

Using Define to Protect Files

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.

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: