0

我必须记录带有“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); 
} 
?>
4

3 回答 3

0

提示: -如果在我们的字符串中,请
更改分隔符:查看列表 -未关闭 - 使用\
\b
file_put_contents

该函数等同于依次调用 fopen()、fwrite() 和 fclose() 将数据写入文件。
如果文件名不存在,则创建文件。除此以外。
使用 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);
  }
 ?>
于 2012-09-19T13:08:11.387 回答
0

为什么不直接使用 strpos?正则表达式对此太过分了。

if (strpos($agent, 'Mozilla/5.0') !== false)
{
   //
}
else
{
   //
}

如果你真的想使用 preg_match 那么这应该工作:

if (preg_match("/^Mozilla\/5\.0/", $agent))  {
于 2012-09-19T13:28:29.237 回答
-1

使用此函数从用户代理获取 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));
}
?>
于 2012-09-19T13:07:51.723 回答