您好,有没有一种方法可以在不检查用户代理的情况下使用 javascript / php 检测浏览器?我正在尝试检测可能伪造用户代理的非人类访问者(套接字和 CURL 尝试)。
不可靠,不。任何人都可以伪造真实的客户。
行为良好的机器人将使用自己的用户代理。无论如何,您都不应该担心行为不端的机器人。
同意用户代理不可靠且容易被欺骗。但是,您也许可以构建一些 JavaScript 来使欺骗变得更加困难。它当然可以区分那些没有 JavaScript 引擎的东西。在这里查看我的答案:https://stackoverflow.com/a/12571513/399704
不仅仅是尝试使用 JavaScript 或通过用户代理字符串来检测它。
我对如何在伪造用户代理字符串时识别浏览器进行了一些研究。我发现许多浏览器(有时版本差异很小,有时版本差异很大)以不同的顺序发送不同的标头信息。一般来说,可以通过检测标头信息发送到服务器的顺序来区分所有大型浏览器(Firefox、IE、Chrome...)。尽管如此,这也可以被欺骗。
有关更多信息,请阅读此处:http://hide.network/why-does-changing-your-user-agent-almost-come-to-nothing/
检测起来有点棘手,但有可能。在 PHP 中,您可以简单地使用函数 getallheaders() 来完成此任务。正如我所测试的,它为您提供的标头信息的顺序与浏览器发送的顺序相同。你只需要检测每个键的实际索引即可。
<?php
foreach (getallheaders() as $name => $value)
{
echo "$name: $value<br />\n";
}
?>
编辑:
我编写了一个脚本来检测 PHP 中的一些主要浏览器。起初我忘记了通过单击页面链接发送的引荐来源网址。我将带有 IE 和 FF 引用的标头添加到列表中,也许这可以帮助解决任何问题。我还将脚本上传到 hide.network / header.php 但无法发布两个以上的链接。
<?php
$headerInformation = array();
// declaring and filling pre-defined header orders of browsers
$browserInformation = array
(
"browserNames" => array
(
"Mozilla Firefox 37.0",
"Mozilla Firefox 37.0 with referer",
"Internet Explorer 11",
"Internet Explorer 11 with referer",
"Internet Explorer 8",
"Google Chrome 42",
"SRWare Iron 37"
),
"headerInformation" => array
(
array("host", "user-agent", "accept", "accept-language", "accept-encoding", "connection", "cache-control"),
array("host", "user-agent", "accept", "accept-language", "accept-encoding", "referer", "connection", "cache-control"),
array("accept", "accept-language", "user-agent", "accept-encoding", "host", "dnt", "connection"),
array("accept", "referer", "accept-language", "user-agent", "accept-encoding", "host", "dnt", "connection"),
array("accept", "accept-language", "user-agent", "accept-encoding", "host", "connection"),
array("host", "connection", "cache-control", "accept", "user-agent", "accept-encoding", "accept-language"),
array("host", "connection", "accept", "user-agent", "accept-encoding", "accept-language")
),
"identScore" => array(0, 0, 0, 0, 0)
);
// parsing all header values
foreach (getallheaders() as $name => $value)
{
array_push($headerInformation, strtolower($name));
}
// calculating possibility for each browser
for($i = 0; $i < count(10); $i++)
{
for($j = 0; $j < count($browserInformation["browserNames"]); $j++)
{
$currentPossibility = count(array_intersect_assoc($browserInformation["headerInformation"][$j], $headerInformation)) / count($headerInformation) * 100;
$currentPossibility = round($currentPossibility, 2);
$browserInformation["identScore"][$j] = $currentPossibility;
}
}
// sort array
array_multisort($browserInformation["identScore"], SORT_DESC, SORT_NUMERIC,
$browserInformation["browserNames"], $browserInformation["headerInformation"]);
// output
for($i = 0; $i < count(10); $i++)
{
for($j = 0; $j < count($browserInformation["browserNames"]); $j++)
{
echo "possibility " . $browserInformation["browserNames"][$j] . ": " . $browserInformation["identScore"][$j] . " %<br />";
}
}
// output original sent header
echo "<pre>";
var_dump($headerInformation);
echo "</pre>";
?>