如何检查 URL 中是否有任何变量被传递给 GET

How to check if any variable is being passed to GET in URL

我遇到这样一种情况,有人试图通过不断向我的网站 URL 发送个人身份信息来破坏我的 google adsense 帐户。我怎样才能阻止这个或至少检测他们正在使用的随机变量?

例如,变量名可以是任何东西。

mysite.com/?asdfd=emailaddress@gmail.com

mysite.com/?gfrewgtr1=电子邮件地址@gmail.com

...?

我唯一能想到的就是收集已知变量,然后执行 header 位置重定向到主站点 URL。

$_GET reserved variable 将包含在 URL 中传递的任何参数。

var_dump($_GET);

将输出:

array(1) {
  ["asdfd"]=>
  string(22) "emailaddress@gmail.com"
}

如果该数组中有任何内容,那么您基本上已经检测到它们了。您可以进一步使用逻辑来清除您可能在您的网站周围使用的已知查询参数,并在您发现任何您认为可行的内容时采取相应的行动。

如果:你想没有GET参数,检查$_GET是否为空

if (!empty($_GET)) {
    header('Location: ' . $_SERVER['SCRIPT_NAME']);
    exit;
}

或者: 检查 $_GET 是否存在不允许的参数:

$allowed_params = ["id", "some_param", "another one"];
foreach($_GET as $key => $val)
    if (!in_array($key, $allowed_params)) {
        // if something's wrong, get out!
        echo('Location: '.$_SERVER['SCRIPT_NAME']);
        exit;
    }

// everything is ok here

Note: before any header()s you mustn't have any output. Otherwise you'll get an error. Better place the code in the very top of your script.