要归档的URL参数

时间:2011-07-21 01:40:06

标签: php

如何使用url参数创建和编写文件

例如: web.com/index.php?name=test.txt?input=halloworld

文件名应为test.txt,其内容必须为halloworld。

这是我到目前为止所拥有的

if(isset($_GET['name']))
{

    $File = $_GET['name']; 
    $Handle = fopen($File, 'w');
    $Data =  $_GET['input']; 
    fwrite($Handle, $Data);
    fclose($Handle); 
}

file output = test.txt?input = halloworld是错误的

谢谢。

2 个答案:

答案 0 :(得分:3)

您的错误是在使用?而不是&在您的网址中,它应为web.com/index.php?name=test.txt&input=halloworld

那说你应该从不做这样的事情,因为这是非常危险的。通过这种方式,您将服务器暴露于任何类型的攻击或破坏。攻击者可以轻松地在您的网络服务器上编写一个php文件并控制它。

记住这一点。

编辑:我在问题评论中写了它,但我在这里重写了,如果攻击者用这个参数调用你的脚本会怎么样:

web.com/index.php?name=evil.php&input=<?php exec('rm -rf /');

然后请求网址

web.com/evil.php

您的文件系统将被清除(如果用户有权执行此操作)。其他类型的攻击可能更微妙,可以在您的服务器中注入php shell,等等。

答案 1 :(得分:1)

web.com/index.php?name=test.txt?input=halloworld不是正确的查询字符串。考虑:

web.com/index.php?name=test.txt&input=halloworld