0

是否可以通过 html 表单发布 PHP 代码,然后将其作为 PHP 运行?

像这样的东西

<form action="#" method="post">
<textarea name="code"></textarea><br>
<input type="submit" value="Run!">
</form>

<?php

if (!empty($_POST['code'])) {
    $_POST['code'];  // Run the Posted PHP code here
}

?>

所以如果我输入

<?php echo 'this is a test'; ?>

在我的文本区域中,然后发送它会回显“这是一个测试”的表单

回答后编辑

谢谢你们,我不知道 eval() 函数,我想我也可以这样做以防止黑客攻击:

<form action="#" method="post">
<textarea name="code"><?php echo $_POST['code']; ?></textarea><br>
<input type="password" name="pass"><br>
<input type="submit" value="Run!">
</form>

<?php

$pass = 'A SHA2 HASHED PASSWORD';
if (!empty($_POST['code']) && $pass == hash('sha384',$_POST['pass'])) {
    eval($_POST['code']);  // Run the Posted PHP code here
}

?>
4

4 回答 4

3

使用 eval() 通常是一个坏主意,我不推荐它。

以下是一些原因:

  • 潜在的不安全输入:传递不受信任的参数是一种失败的方式。确保参数(或其中的一部分)被完全信任通常不是一项简单的任务。

  • 技巧:使用 eval() 使代码更聪明,因此更难遵循。引用 Brian Kernighan 的话:“首先,调试的难度是编写代码的两倍。因此,如果你尽可能巧妙地编写代码,那么根据定义,你就不够聪明,无法调试它”

(来自#951373

如果你真的应该,你可以像这样使用它:


<?php

if (!empty($_POST['code'])) 
{
    $code = $_POST['code']
    eval($code);
}

?>

我希望这有帮助。

于 2013-06-28T12:53:17.470 回答
2

可以使用eval();函数http://php.net/eval

eval($_POST['code']);  // Run the Posted PHP code here

请记住,这经常被黑客滥用(代码注入)。

如果您想在旅行中使用 PHP 而无需上传或登录,

我强烈建议 php codepad 不要冒险使用您的服务器:https ://www.google.com/search?q=codepad+php&ie=utf-8&oe=utf-8&aq=t

于 2013-06-28T12:42:22.083 回答
2
<form action="#" method="post">
<textarea name="code"></textarea><br>
<input type="submit" value="Run!">
</form>

<?php

if (!empty($_POST['code'])) {
    eval($_POST['code']);  // Run the Posted PHP code here
}

?>
于 2013-06-28T12:43:33.227 回答
1

您还可以通过保持操作标记与 .php 文件的名称相同来回溯。

于 2013-06-28T12:45:37.557 回答