Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

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


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.