破解PHP脚本 - fwrite和变量

时间:2013-04-28 19:47:05

标签: php string variables fwrite

通过以下PHP脚本处理$ _POST数据,其中'mike'和'tampa'是帖子数据:

<?php
$name = $_POST['name'];
$city = $_POST['city'];

$fh = fopen('variables.php', 'w') or die("can't open file");
fwrite($fh,"<?php \n");
fwrite($fh,"\$name = $name");
fwrite($fh,$city);
fwrite($fh,"\n ?>");
fclose($fh);
header("Location: main.php");
?>

导致以下内容被写入variables.php文件:

<?php 
$name = miketampa
 ?>

但是,我最终需要做的是输出如下:

<?php 
$name = mike;
$city = tampa;
 ?>

我花了几天时间试图找出如何通过反复试验使其正常工作但我无法让它发挥作用。所以这里有我遇到的3个问题,请有人告诉我(告诉我)正确的方法让它发挥作用。

  1. 如果我将$ name的FWRITE代码复制到$ city行,PHP会中断,为什么?
  2. 我无法让PHP在$ name结尾处接受分号,即使我试图逃避;字符
  3. 我不能让PHP在FWRITE写的字符串末尾接受\ n(换行符)
  4. 我已经包含了以下代码,以防万一有人想看到我弄乱的所有内容。感谢您的帮助。**

    form.php的

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>
    
    <body>
    <form action="process.php" method="post">
    What is your name?<input name="name" type="text" />
    what city do you live in?<input name="city" type="text" />
    <input type="submit" name="submit" id="submit" value="Submit" /></form>
    
    </body>
    </html>
    

    PROCESS.PHP

    <?php
    $name = $_POST['name'];
    $city = $_POST['city'];
    
    $fh = fopen('variables.php', 'w') or die("can't open file");
    fwrite($fh,"<?php \n");
    fwrite($fh,"\$name = $name");
    fwrite($fh,"\$city = $city");
    fwrite($fh,"\n ?>");
    fclose($fh);
    header("Location: main.php");
    ?>
    

    VARIABLES.PHP - 除了process.php写入的内容之外是空的

    MAIN.PHP

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    </head>
    <?php include ('variables.php') ?>
    <body>
    <div>Hello world, my name is <?=$name?> and I live in <?=$city?></div>
    </body>
    </html>
    

1 个答案:

答案 0 :(得分:0)

请参阅标记的行以进行更改:

fwrite($fh,"\$name = '$name';\n"); // <-- change 
fwrite($fh,"\$city = '$city';"); // <-- change

这应该可以解决问题!

;$name之后看到我的修改:$city! 第二次修改:将$name$city括在'(单引号)