如何检测用户的浏览器和操作系统并使用它从 PHP 页面动态修改我的 MySQL 数据库查询
如果用户在特定操作系统上使用特定浏览器,我需要能够运行 mysql 查询并忽略某些结果。我的数据库查询是从 AMFPHP 服务进行的,我有一个 javascript browserDetect 脚本,可以用来获取用户浏览器和操作系统。
理想情况下,我想在用户到达我的页面时运行 browserDetect 脚本,然后将其存储在 PHP 会话变量中,但我不知道如何将 javascript 变量传递到 PHP 会话。这将允许我的数据库查询脚本查找会话并相应地修改查询。我还尝试将浏览器检测脚本包含在 AMFPHP 脚本中,但这不起作用 - 我意识到这是服务器端与客户端问题。
还有哪些其他解决方案适合我?
谢谢
I need to be able to run a mysql query and omit certain results if the user is using a certain browser on a certain OS. My database query takes place from an AMFPHP service and I have a javascript browserDetect script that I can use to get user browser and OS.
Ideally I want to run the browserDetect script when the user arrives at my page and then store this in the PHP session variables but I cannot see how to pass the javascript vars to the PHP session. This would allow my database query script to look up the session and modify the query accordingly. I also tried to include the browser detect script in the AMFPHP script but this did not work - I realise that this a serverside vs client side problem.
What other solutions would work for me?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不只使用 PHP 来实现这一点呢? PHP 还能够获取浏览器和操作系统...
$_SERVER['HTTP_USER_AGENT']
包含浏览器和操作系统...Why not accomplish that with PHP only? PHP is also capable of fetching browser and os...
$_SERVER['HTTP_USER_AGENT']
contains browser and os...正如 @thedom 所说,只需在 PHP 中使用
$_SERVER['HTTP_USER_AGENT']
即可。您的用户代理(除非用户修改)将包含浏览器、浏览器版本和操作系统。这是我的:
但是如果你想使用 javascript,你就必须做一些类似于我实现 ajax 登录所做的事情。
我基本上把它放在脚本的顶部(login.php)
并且在页面中运行javascript以检测会话(index.php)
然后我发布到另一个脚本(login.php)(要记录在这种情况下,您通过ajax)
使用JQuery。
As @thedom said, just use
$_SERVER['HTTP_USER_AGENT']
in PHP. Your user agent (Unless modified by the user) will contain the browser, browser version and os.Here's mine:
BUT if you want to use javascript, you'll have to do something similar to what I did to implement ajax logins.
I basically put this at the top of the script (login.php)
And this in the page the javascript will be run from to detect session (index.php)
And then I post to the other script (login.php) (To log you in via ajax in this case)
Using JQuery.
您可以将 browserDetect javascript 的结果设置为 javascript cookie,并且 PHP 能够从该
详细信息中读取 cookie : 如何使用 PHP 读取 Javascript cookie
you can set the results from browserDetect javascript to javascript cookie, and PHP is able to read cookie from that
details : How to read Javascript cookie with PHP
另一种解决方案是让 javascript 发出包含操作系统/浏览器的 ajax 请求,但更好的方法可能是解析用户代理。
Another solution would be to let the javascript make an ajax request containing OS/Browser, yet the better way might be to parse the user agent.