0

私は使用しています:

<?php

$file = $_GET['page']; //ie: http://localhost/?page=index
include "php/{$file}.php";

?>

$fileしかし、 を呼び出す前に変数をトリムする最も安全な方法を知る必要がありますinclude

私は次のようなことを考えていました:

//-replace '.' with ''
//-replace '/' with ''
//-replace '\' with ''

でも、もっといい方法があると思いますので、よろしくお願いします。私はそれが非常に安全である必要があります。

ありがとうございました。

4

2 に答える 2

2

非常に安全にする必要がある場合は、トリミング後であっても、そのままでは含めないでください。代わりに、取得する変数と含める必要のあるファイルの間に辞書のような関連付けを使用します。

<?php
$file = $_GET['page'];

$allowed_pages = array('index', 'profile', 'legal', 'about');

if (in_array($file, $allowed_pages)) {
    include "php/" . $file . ".php");
} else {
    die("The page " . $file ." does not exist");
}
?>

このようにして、予期しないインクルードが発生することはありません。

于 2012-10-24T07:00:45.373 に答える
1

何をしても、ユーザー データは信頼できないと思います。

利用可能なファイルの数が限られている場合は、それらの配列を作成して許可する必要があります。

$allowedFiles = array('index','somewhere','elswhere');

if (!in_array($file,$allowedFile))
    exit ('File Not found');
else
    include("php/{$file}.php");

編集:

もう 1 つの方法は、ディレクトリをスキャンして、不要なファイルとディレクトリを削除することです。

$filesInDir = scandir("./php");
$allowedFiles = array_diff(array(".","..","protected.php"),$filesInDir);

if (!in_array($file.".php",$allowedFile))
    exit ('File Not found');
else
    include("php/{$file}.php");
于 2012-10-24T07:00:17.770 に答える