我必须记录用户代理“Mozilla / 5.0”附带的IP地址(之后没有webkit等。显式只是Mozilla / 5.0)或null。我一直在玩preg_match,但没有运气好。我知道这不是最整洁的代码,但它只是试图完成工作。我需要对preg_match进行哪些更改才能使其正常工作?
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$date = date("H:i dS F");
$file = "log.html";
if (preg_match("/\bMozilla\/5.0/", $agent)) {
$open = fopen($file, "a+");
fwrite($open, $ip . " | ".$agent." | ". $date);
fclose($open);
} elseif (preg_match("/Null/", $agent)) {
$open = fopen($file, "a+");
fwrite($open, $ip . " | ".$agent." | ". $date);
fclose($open);
}
?>
答案 0 :(得分:0)
提示:
- 如果我们的字符串中有\
,请更改分隔符:请参阅list
- \b
未被关闭
- 使用file_put_contents
此函数与调用fopen(),fwrite()和fclose()相同 先后将数据写入文件 如果filename不存在,则创建该文件。否则,
使用FILE_APPEND标志将内容附加到文件的末尾
和LOCK_EX标志,以防止其他人同时写入该文件
试
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$date = date("H:i dS F");
$file = "log.html";
if (preg_match("#\bMozilla/5.0\b#", $agent))
{
var_dump($agent);
//return 'Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0'
$content = " $ip | $agent | $date ";
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
}
?>
答案 1 :(得分:0)
为什么不使用strpos?正则表达式对此有点过分。
if (strpos($agent, 'Mozilla/5.0') !== false)
{
//
}
else
{
//
}
如果你真的想使用preg_match,那么这应该可行:
if (preg_match("/^Mozilla\/5\.0/", $agent)) {
答案 2 :(得分:-1)
使用此功能从useragent获取IP
<?php
# validip/getip courtesy of manolete <manolete@myway.com>
# IP Validation
function validip($ip) {
if (!empty($ip) && $ip==long2ip(ip2long($ip))) {
# reserved IANA IPv4 addresses
# http://www.iana.org/assignments/ipv4-address-space
$reserved_ips = array (
array('0.0.0.0','2.255.255.255'),
array('10.0.0.0','10.255.255.255'),
array('127.0.0.0','127.255.255.255'),
array('169.254.0.0','169.254.255.255'),
array('172.16.0.0','172.31.255.255'),
array('192.0.2.0','192.0.2.255'),
array('192.168.0.0','192.168.255.255'),
array('255.255.255.0','255.255.255.255')
);
foreach ($reserved_ips as $r)
if ((ip2long($ip) >= ip2long($r[0])) && (ip2long($ip) <= ip2long($r[1])))
return false;
return true;
}
return false;
}
/* Patched function to detect REAL IP address if it's valid */
function getip() {
if (getenv('HTTP_CLIENT_IP') && long2ip(ip2long(getenv('HTTP_CLIENT_IP')))==getenv('HTTP_CLIENT_IP') && validip(getenv('HTTP_CLIENT_IP')))
return getenv('HTTP_CLIENT_IP');
if (getenv('HTTP_X_FORWARDED_FOR') && long2ip(ip2long(getenv('HTTP_X_FORWARDED_FOR')))==getenv('HTTP_X_FORWARDED_FOR') && validip(getenv('HTTP_X_FORWARDED_FOR')))
return getenv('HTTP_X_FORWARDED_FOR');
if (getenv('HTTP_X_FORWARDED') && long2ip(ip2long(getenv('HTTP_X_FORWARDED')))==getenv('HTTP_X_FORWARDED') && validip(getenv('HTTP_X_FORWARDED')))
return getenv('HTTP_X_FORWARDED');
if (getenv('HTTP_FORWARDED_FOR') && long2ip(ip2long(getenv('HTTP_FORWARDED_FOR')))==getenv('HTTP_FORWARDED_FOR') && validip(getenv('HTTP_FORWARDED_FOR')))
return getenv('HTTP_FORWARDED_FOR');
if (getenv('HTTP_FORWARDED') && long2ip(ip2long(getenv('HTTP_FORWARDED')))==getenv('HTTP_FORWARDED') && validip(getenv('HTTP_FORWARDED')))
return getenv('HTTP_FORWARDED');
$ip = htmlspecialchars($_SERVER['REMOTE_ADDR']);
/* Added support for IPv6 connections. otherwise ip returns null */
if (strpos($ip, '::') === 0) {
$ip = substr($ip, strrpos($ip, ':')+1);
}
return long2ip(ip2long($ip));
}
?>