tags:

views:

75

answers:

4
+1  Q: 

User input include

This is pretty darn safe right? Something I've missed?

$page = sprintf("%s/%s.php", "pages", $_GET['page']);
if (file_exists($page)) {
 include $page;
}
else {
 echo "The page '$page' does not exist =(";
}

(yes you can use it)

+4  A: 

It's extremely unsafe since a user can arbitrarily load whatever page they feel like.

byte
A: 

if you are sure there's no / or .. in $_GET['page'], and that the visitor is allowed to view any php file in the pages dir, i think it's okay.

p4bl0
+4  A: 

The "better" way to do this is to have an array of the allowed pages, then do something like this:

$page = $_GET['page'] . '.php';
if(in_array($page, $all_pages)) {
    include('pages/' . $page);
}

You could easily get a list of all allowed pages by doing something like this:

$all_pages = glob('pages/*.php');

Documentation: in_array, glob

Paolo Bergantino
thats smart, thanks
A: 

Use basename() to make sure there's no path information being supplied :

$page = sprintf("%s/%s.php", "pages", basename($_GET['page']));
if (file_exists($page)) {
        include $page;
}
else {
        echo "The page '$page' does not exist =(";
}
Ken Keenan