code_junkie

What's the best way to only allow a PHP file to be included?

Nov 11th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. $_SERVER['SCRIPT_FILENAME']
  2.  
  3. .htaccess
  4.  
  5. php.ini
  6.  
  7. php.ini
  8.  
  9. $parentPath = dirname(dirname(__FILE__));
  10. $ourPath = $parentPath . DIRECTORY_SEPARATOR . 'include';
  11.  
  12. $includePath = ini_get('include_path');
  13. $includePaths = explode(PATH_SEPARATOR, $includePath);
  14. // Put our path between 'current directory' and rest of search path
  15. if ($includePaths[0] == '.') {
  16. array_shift($includePaths);
  17. }
  18.  
  19. array_unshift($includePaths, '.', $ourPath);
  20. $includePath = implode(PATH_SEPARATOR, $includePaths);
  21. ini_set('include_path', $includePath);
  22.  
  23. $parentPath = dirname(dirname(__FILE__));
  24. $ourPath = $parentPath . DIRECTORY_SEPARATOR . 'include';
  25.  
  26. $includePath = ini_get('include_path');
  27. $includePaths = explode(PATH_SEPARATOR, $includePath);
  28. // Put our path between 'current directory' and rest of search path
  29. if ($includePaths[0] == '.') {
  30. array_shift($includePaths);
  31. }
  32.  
  33. array_unshift($includePaths, '.', $ourPath);
  34. $includePath = implode(PATH_SEPARATOR, $includePaths);
  35. ini_set('include_path', $includePath);
  36.  
  37. <?php
  38. if (!eregi("modules.php", $PHP_SELF)) {
  39. die ("You can't access this file directly...");
  40. }
  41. // more code ...
  42. ?>
  43.  
  44. if(!isset($in_prog)){
  45. exit;
  46. }
  47.  
  48. // index.php
  49. require_once 'includes.php';
  50.  
  51. // includes.php
  52. define('IN_MY_PROJECT', true);
  53. include 'myInc.php';
  54.  
  55. // myInc.php
  56. defined('IN_MY_PROJECT') || die("No direct access, plsktnxbai");
Add Comment
Please, Sign In to add comment