We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

skywalker on 05/15/08


Tagged

file include protect


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

digiloper


Include File Protection ..


Published in: PHP 


URL: http://haugland.ca/?p=tutorial&i=19

include file protection...

  1. Using includes in PHP to simplify life? Don't want your visitors to access them and receive errors or partial content? The solution is simple enough, though many people don't worry or don't consider it as a "threat".
  2. There are a few reasons you wouldn't want someone to go through directories trying to access your include files. Whatever yours might be, there's an easy way to prevent it.
  3. On your index file, you have it generate a variable.
  4. <?php
  5. $include_lock = "unlocked";
  6. ?> So we now have some code that generates a variable, '$include_lock' with the value 'unlocked'. Why are we doing this? Well, since you only want them to access the page through the index, we'll make a lock and key so that only going through the index file gives them the key.
  7. With the code above, we need to now put something on the page that actually performs as the lock. It's simple enough, we'll just use a nice, clean if statement.
  8. <?php
  9. if ($include_lock != "unlocked") {
  10. //Shut them down.
  11. header("Location: 404.shtml");
  12. OR
  13. die("404 - File Not Found");
  14. //You basically want it so that it would look like the typical 404 message from your site.
  15. } else {
  16. //your normal content goes here.
  17. }
  18. ?> You'll want to take note that if you have 'register_globals' on, the user could simply add '?include_lock=unlocked' to the URL and gain access. To get around that, you could disable register_globals or use a session variable. If you go the session route, kill the variable after you do the include.

Report this snippet 

You need to login to post a comment.