2012-09-19 34 views
0

我不得不記錄用戶代理「Mozilla/5.0」(沒有webkit等,明確只是Mozilla/5.0)或null的IP地址。我一直在玩preg_match,但沒有好運。我知道這不是最簡單的代碼,但它只是試圖完成工作。我需要對preg_match進行哪些更改才能正常工作?HTTP_USER_AGENT上的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

提示:
- 改變分隔符如果\在我們的字符串:看list
- \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); 
    } 
?> 
-1

使用此功能從用戶代理獲取IP

<?php 
# validip/getip courtesy of manolete <[email protected]> 
# 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)); 
} 
?> 
0

爲什麼不只是使用strpos?正則表達式對此是矯枉過正的。

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

如果你真的想使用的preg_match那麼這應該工作:

if (preg_match("/^Mozilla\/5\.0/", $agent)) {