使用 PHP 有条件地记录标头中的值
我对 PHP 还很陌生,所以请耐心等待。
我正在尝试记录访问我的网站的手机的用户代理。我通过检查 $_SERVER['HTTP_USER_AGENT'] 值来执行此操作。
遇到一个小问题,我注意到如果该人通过 Opera Mini 访问我的网站,那么 Opera Mini 会将实际的用户代理移动到标识为 X-OperaMini-Phone-UA 的标头值中:
所以我正在寻找 PHP 代码将从标头中提取此信息(如果存在),如果没有,则将其标识为 NA 以用于数据库日志记录。
这是我目前用于日志记录的代码,它是由其他人编写的
<?php
mysql_connect('server.com', 'dbuser', 'dbpass');
$url = mysql_real_escape_string("http://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]");
$fn = mysql_real_escape_string($_SERVER['SCRIPT_NAME']);
$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$ref = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
$ua = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
mysql_select_db('db');
mysql_query("INSERT INTO `record` VALUES ('$url', '$fn', NOW(),'$ip', '$ref','$ua')");
?>
所以,现在我添加了一些名为 opera_user_agent 的字段,我想记录 Opera 用户代理(如果有)并使用“NA”(如果没有) 't。
I'm pretty new to PHP so please bear with me.
I'm trying to log the User Agents for mobile phones accessing my site. I'm doing this by checking the $_SERVER['HTTP_USER_AGENT'] value.
Ran into a small issue where I noticed if the person was accessing my site via opera mini then opera mini moves the actual user agent into a header value identified as X-OperaMini-Phone-UA:
So I'm looking for the PHP code which will extract this from the header (if present) and if not, identify it as NA for database logging purposes.
This is the code I'm currently using for logging which was written by someone else
<?php
mysql_connect('server.com', 'dbuser', 'dbpass');
$url = mysql_real_escape_string("http://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]");
$fn = mysql_real_escape_string($_SERVER['SCRIPT_NAME']);
$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$ref = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
$ua = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
mysql_select_db('db');
mysql_query("INSERT INTO `record` VALUES ('$url', '$fn', NOW(),'$ip', '$ref','$ua')");
?>
So, now I'm adding a few field named opera_user_agent and I want to log the Opera user agent if there is one and use 'NA' if there isn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 getallheaders 获取值:
另一种方法是添加标头到 .htaccess 中具有重写规则的环境变量,然后像往常一样通过
$_SERVER
获取它:You can fetch the value with getallheaders:
Another approach is to add the header to an environment variable with a rewriterule in .htaccess, and then pick it up via
$_SERVER
as usual:您也可以使用
$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']
:You can use
$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']
as well:2012 年 10 月,Opera 建议使用名为
Device-Stock-UA
的新标头。此后,新的 Opera Mini/Mobile 浏览器将使用新的Device-Stock-UA
和旧的X-OperaMini-Phone-UA
标头。https://dev.opera.com/blog/introducing-device-stock- ua/
这个参数定义如下:
关于该更新我创建了这个函数:
On October 2012 Opera suggested new header called
Device-Stock-UA
. After that time new Opera Mini/Mobile browsers will use newDevice-Stock-UA
and oldX-OperaMini-Phone-UA
headers.https://dev.opera.com/blog/introducing-device-stock-ua/
This parameter is defined like this:
Regarding to that update I created this function: