如何在 PHP 中获取用户代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10243841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:44:00  来源:igfitidea点击:

How to get user agent in PHP

phpuser-agent

提问by Olga Budnik

I'm using this JS code to know what browser is user using for.

我正在使用此 JS 代码来了解用户使用的浏览器。

<script>
  document.write(navigator.appName);
</script>

And I want to get this navigator.appName to php code to use it like this:

我想把这个 navigator.appName 放到 php 代码中,像这样使用它:

if ($appName == "Internet Explorer") {
  // blabla
}

How can I do it?

我该怎么做?

回答by noli

Use the native PHP $_SERVER['HTTP_USER_AGENT']variable instead.

请改用原生 PHP$_SERVER['HTTP_USER_AGENT']变量。

回答by Aurovrata

You could also use the php native funcion get_browser()

您也可以使用 php 本机功能get_browser()

IMPORTANT NOTE: You should have a browscap.ini file.

重要说明:您应该有一个 browscap.ini 文件。

回答by arthur86

You can use the jQuery ajax method linkif you want to pass data from client to server. In this case you can use $_SERVER['HTTP_USER_AGENT']variable to found browser user agent.

如果要将数据从客户端传递到服务器,可以使用 jQuery ajax 方法链接。在这种情况下,您可以使用$_SERVER['HTTP_USER_AGENT']变量来找到浏览器用户代理。