1

PHPは、fwriteを使用してファイルに書き込む前に、引用符を自動的にエスケープします。テストコードページを作成しようとしています。これが私が持っているコードです:

<?php
if ($_GET['test'] == 'true') {
$code = $_POST['code'];
$file = fopen('testcode.inc.php', 'w+');
fwrite($file, $code);
fclose($file);
require_once('testcode.inc.php');
}
else {
echo "
<form method='post' action='testcode.php?test=true'>
<textarea name='code' id='code'></textarea><br><br>
<button type='submit'>Test!</button><br>
</form>
";
}
?>

フォームに次のように入力すると、次のようになります。

<?php
echo 'test';
?>

次のようにファイルに保存されます。

<?php
echo \'test\';
?>

PHPが自動的に引用符をエスケープするのはなぜですか?

4

4 に答える 4

2

それはfwriteではなく、その$ _POST

これらの知識があれば、ここで答えを見つけてください。

だからあなたがしなければならないのはほんの小さな修正です:

if (get_magic_quotes_gpc()) {
  $code = stripslashes($_POST['code']);
}else{
  $code = $_POST['code'];
}
于 2012-05-03T23:24:52.250 に答える
1

magic_quotesが有効になっているため、それを実行しているのはfwriteではありません。

php.iniファイルでマジッククォートを無効にできない場合は、実行時に無効にすることができます。PHPの簡単なビットがすべての入力配列をループし、不要なスラッシュを取り除きます。その後、どのPOST/GETについて心配する必要はありません。ストリップするキー。マジッククォートを無効にする

<?php
if (get_magic_quotes_gpc()) {
    function stripslashes_gpc(&$value)
    {
        $value = stripslashes($value);
    }
    array_walk_recursive($_GET, 'stripslashes_gpc');
    array_walk_recursive($_POST, 'stripslashes_gpc');
    array_walk_recursive($_COOKIE, 'stripslashes_gpc');
    array_walk_recursive($_REQUEST, 'stripslashes_gpc');
}
?>
于 2012-05-03T23:24:44.687 に答える
0

魔法の引用符が有効になっています。ファイルでそれらを無効にするかphp.inimagic_quotes_gpc=off)、またはパススルーし$_POST['code']ますstripslashes

于 2012-05-03T23:25:43.087 に答える