I assumed the included pages were html only, for different elements displayed in a div, based on the original request.
Then the pages should be included as HTML, rather than processed with PHP. PHP is, however, more useful.
Wouldn't (strpos(a,b)) when b does exist as the first character evaluate to true?
No. strpos() can return two types: boolean false, if the character is not found at all, or the position of the character in the string. The first position in an array, string, &c. in PHP is 0; therefore, the first character is at position 0.
If not, then--
echo "" ? '' : (0 ? '' : 'PHP is weird.');
Not really. The indexing position is the same in most languages, but generally they have a less confusing "not found" value, such as null or -1.
Also, really, you don't need the basename() function now that I think about it.
Not if you do it my way, with the path check.
Simply use the older version of the code, and it will be sure the file exists before including.
No, even without the NUL bug it's possible to read any file on the filesystem that ends with .txt, which is a potential security risk. If you wanted subdirectories, it's possible to simply remove the check for /:
Code:
<?php
function inc() {
$page = isset($_GET['page'])
? $_GET['page']
: 'default';
if(strpos($page, '.') !== false
|| !file_exists($page = 'includes/' . $page . '.inc.php'))
$page = 'includes/error.inc.php';
include($page);
}
?>
The check for . will prevent paths with .. in them. Another option is to do:
Code:
<?php
function inc() {
$page = isset($_GET['page'])
? $_GET['page']
: 'default';
if(strpos($page, './') !== false
|| !file_exists($page = 'includes/' . $page . '.inc.php'))
$page = 'includes/error.inc.php';
include($page);
}
?>
This also allows files with dots in the path, whilst at the same time disallowing paths such as ../file and ././././file, the latter of which is a commonly-used measure for bypassing buggy security systems. It's technically possible to have directories with . at the end of their names, but I've never seen one.
Bookmarks