Forum Moderators: coopster
A few questions:
1 - Would it be better (more secure, harder to hack, that sort of thing) to have the included pages outside of the web path on my server. Ex: instead of include("includes/file.php") use include("/home/username/someotherdirectory/file.php"). I'm thinking this would keep "casual users" from just browsing to the file. I've already got code to redirect if any of the includes is opened directly, but figured this could add a little something else. I've done this on my development server and it seems to work, but is there anything I need to watch out for?
2 - Is it wise to keep the .php extension on the include files? I have been so that the file is parsed just in case someone tries to open it directly.
Thanks,
- Ryan
1) Yes, better to keep includes out of webroot, but for a small personal site maybe not such a big deal, as long as they don't contain real key info like username and password. In that case, if your host happens to run the webserver with php turned off, you'll be sorry.
You can keep users from getting any meaningful output from include files by defining a constant
define('INSIDE_SCRIPT', 1); if(!defined('INSIDE_SCRIPT')) die('unauthorized access'); register_globals, there's that small chance people could set this variable through a GET request. I sometimes, before the
die(), first send the headers that Apache sends with 404's, and also set the die string to the Apache 404 HTML output.
2) Yes, make them .php; .inc and other extensions aren't parsed by default if they're called independently, so if there's any concern at all about users surfing these files, make them .php .
I sometimes uses prefixes instead as in
cl_ClassName1.php
cl_ClassName2.php
T_templateName1.php
T_templateName2.php
I only bother to put security-related items outside of webroot, but then I usually have things set up with mod_rewrite so that people can't just request random files anyway and pretty much can't request any file with extensions other than .pdf, .jpg etc, but only valid page URLs
Tom
- I usually rewrite all requests to index.php
- I don't have file extensions in the URLs at all
- I have some mechanism for checking whether or not the request is a valid request on the site (limited to a certain dir, the url is in a DB of pages or whatever).