2012-01-12 59 views
0

如果我有这样的指标:查询字符串动态内容

if (isset($_GET['se'])) { 
    $se= $_GET['se']; 

    if (file_exists("{$se}.php")){ 
     require("{$se}.php"); 
    } 
    else { 
     require("page_error.php"); 
    } 
} 
else { 
    require("page_error.php"); 
} 

类似下面的链接不起作用:

$pwrurl = "http://example.com/login/?se=change_password?usermail=".$email."&usercode=".$linkHash; 

只有这样的:http://example.com/login/?se=change_password将被接受。

这能解决吗?

+4

尝试使用'&'作为分隔符。你有两个'?'这是不正确的。 – 2012-01-12 22:37:04

+0

你的意思是'动态'(而不是动态)? – 2012-01-12 22:37:54

回答

5

当心!

让用户决定在没有任何验证的情况下包含哪个文件会给您的服务器带来一个漏洞。他们可以将脚本指向任何敏感文件。

你应该限制可包括哪些准备,就像这样:

$allowed_files = array(
    "page_error", 
    "some_section", 
    "some_other_section", 
    "change_password" 
    ); 

$se = empty($_GET['se']) ? "page_error" : $_GET['se'] ; // "page_error" by default. 

if (in_array($se, $allowed_files)){ 
    require("{$se}.php"); 
} else { 
    require("page_error.php"); 
} 

这样,他们只能读取你把数组中的文件。

编辑:另外,就像其他人说的一样,你应该在URL中分开不同的参数=参数对,而不是使用&。这个?用于从参数列表中分离页面名称。

http://example.com/login/?se=change_password&usermail=... 
+0

好的谢谢。我也将使用FILTER_VALIDATE_URL – 2012-01-12 22:55:40

+0

使用该过滤器,但不要仅依赖于该过滤器,因为它不会保护您免受我提到的漏洞攻击。 – 2012-01-13 01:05:03

3

您在URL中有两个?。多个参数必须用&分开。

您使用require非常危险。阅读安全。在将其传递给这样一个危险函数之前验证任何参数,否则您的网站将很快被黑客入侵。

2

该链接是错误的,应该是'&'而不是'?'在change_password之后。

$pwrurl = "http://example.com/login/?se=change_password&usermail=".$email."&usercode=".$linkHash;