I just str_replace "/" -- no slashes, no directory changing -- no slashes, periods won't actually change directory either, it just breaks the path...
Cups method of checking against an array is a REALLY good idea, and I would suggest that what you do is use "glob" to make a list of valid choices. Just put all your sub-files that are valid choices in a directory (keeping it 755 or even 644) and then glob in your list.
Code:
$dirList=glob('includes/*.php');
$includeList=array();
foreach ($dirList as $data) $includeList[]=pathinfo($data,PATHINFO_FILENAME);
...
Then later on just:
Code:
if ($target=array_search($_POST['load'],$includeList) {
include('includes/',$includeList[$target],'.php');
}
...
This means NOT sending $_POST to the include, and instead the value pulled from the filesystem -- that way no comparison trickery can be pulled to trick you into sending a invalid $_POST to the include or to file_exists.
Which is probably as secure as you're gonna get on doing that. It also means that by comparing to the proper list and using the proper list, you don't have to bother stripping out slashes or periods -- as they aren't in the set.
Another approach that might have some merit is to set them as the key in an array, as then you don't need to search, just check isset.
Code:
$dirList=glob('includes/*.php');
$includeList=array();
foreach ($dirList as $data) $includeList[pathinfo($data,PATHINFO_FILENAME)]=true;
if (isset($includeList[$_POST['load']])) {
include('includes/',$includeList[$_POST['load']],'.php');
}
Either way is a decent approach.
Bookmarks