dongshan4316 2013-04-28 19:47
浏览 37
已采纳

破解PHP脚本 - fwrite和变量

Processing $_POST data through the following PHP script with 'mike' and 'tampa' being the post data:

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

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

results in the following being written to the variables.php file:

<?php 
$name = miketampa
 ?>

However, what I ultimately need it to do is output as follows:

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

I have spent several days trying to work out how to get this to work properly through trial and error but I just can't get it to work. So here are the 3 problems I have and please someone tell me (show me) the correct way to get this to work.

  1. if I duplicate the FWRITE code for $name to the $city line the PHP breaks, why?
  2. I cannot get PHP to accept a semi-colon at the end of $name even when I try to escape the ; character
  3. I can't get the PHP to accept (line break) at the end of the string being written by FWRITE

I have included the code below just in case someone wants to see all of what I'm messing up. Thanks for any Help.**

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 
");
fwrite($fh,"\$name = $name");
fwrite($fh,"\$city = $city");
fwrite($fh,"
 ?>");
fclose($fh);
header("Location: main.php");
?>

VARIABLES.PHP - Is empty except what the process.php writes to it

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条回答 默认 最新

  • dongshungou7699 2013-04-28 19:50
    关注

    See marked lines for changes:

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

    This should do the trick!

    see my edit: ; after $name and $city!
    second edit: enclose $nameand $cityin '(single quotes)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备