[PHP] Protecting include()ed files

Status
Not open for further replies.

Tornadus

Ars Scholae Palatinae
1,214
Depending on how the scripts are written, you can also set it up so that only one script is directly accessible from the web.

/var/www/index.php
Code:
<?php

  set_include_path( get_include_path( )
                  . PATH_SEPARATOR
                  . '/etc/myScripts' );

  include( 'include1.php' );
  include( 'include2.php' );

?>

/etc/myScripts/include1.php
/etc/myScripts/include2.php
/etc/myScripts/include3.php
 

topham

Ars Legatus Legionis
13,756
Subscriptor
Tornadus":1xsj3fua said:
Depending on how the scripts are written, you can also set it up so that only one script is directly accessible from the web.

/var/www/index.php
Code:
<?php

  set_include_path( get_include_path( )
                  . PATH_SEPARATOR
                  . '/etc/myScripts' );

  include( 'include1.php' );
  include( 'include2.php' );

?>

/etc/myScripts/include1.php
/etc/myScripts/include2.php
/etc/myScripts/include3.php

If you go to that effort, do yourself a further favour and turn it into a broker. You can parse the URLs, validate the standard inputs and apply security consistently to them.
 
I try to keep as many files out of the webroot as possible (in most projects I do as topham suggests).

Supposing there's a glitch with your kit, and your webserver renders the content of your PHP files, rather than parsing them - you'd potentially be leaking config details (eg. DB auths) to world + dog.

Yes, it's not that likely, but defense in depth works that way.
 
Status
Not open for further replies.