« VisualSVNを入れてみた | メイン | アクセスログは非同期で取るべきか »

User-Agent解析プログラム

アクセス統計を取ろうと思い立ったは良いが、ユーザーのUser-Agent文字列が実にフリーダムな書式で書かれていてブラウザとOSを取得するのにエラい苦労しました。

せっかくなので、その苦労の結果をPHPコードとして残して置こうと思います。

言語: PHP5
返却値: 配列 ([0]:ブラウザ名 [1]:ブラウザバージョン [2]:OS [3]:OSバージョン)

※まだまだ調整中なのでそのまま使うとバージョン情報が取れない事が多いかと。

function useragent_analyser() {
  $ua_string = $_SERVER["HTTP_USER_AGENT"];
  $bws = "";    // [0]
  $bwsver = ""; // [1]
  $os  = "";    // [2]
  $osver = "";  // [3]
  
  // ブラウザ判定
  if (preg_match("/Opera[\s\/]\d\.\d{1,2}/", $ua_string, $matches)) {
    $bws = "Opera";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/MSIE[\s\/]\d\.\d{1,2}/", $ua_string, $matches)) {
    $bws = "MSIE";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/Firefox[\s\/]\d\.\d/", $ua_string, $matches)) {
    $bws = "Firefox";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/Chrome[\s\/]\d\.\d/", $ua_string, $matches)) {
    $bws = "Chrome";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/Safari/", $ua_string, $matches)) {
    $bws = "Safari";
    if (preg_match("/Version[\s\/]\d\.\d/", $ua_string, $saf_matches)) {
      $bwsver = substr($saf_matches[0], strlen("Version") + 1, 1);
    } else if (preg_match("/AppleWebKit[\s\/]4\d{2}/", $ua_string, $saf_matches)) {
      $bwsver = 2;
    }
  } else if (preg_match("/Sleipnir[\s\/]\d\.\d/", $ua_string, $matches)) {
    $bws = "Sleipnir";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/Konqueror[\s\/]\d\.\d/", $ua_string, $matches)) {
    $bws = "Konqueror";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (stristr($ua_string, "Netscape") !== false) {
    $bws = "Netscape";
    if (preg_match("/Netscape\d/", $ua_string, $matches)) {
      $bwsver = substr($matches[0], strlen($bws), 1);
    } else if (preg_match("/Netscape[\s\/]\d\.\d{1,2}/", $ua_string, $matches)) {
      $bwsver = substr($matches[0], strlen($bws) + 1, 1);
    }
  } else if (preg_match("/DoCoMo[\s\/]\d\.\d/", $ua_string, $matches)) {
    $bws = "DoCoMo";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/KDDI/", $ua_string, $matches)) {
    $bws = "KDDI";
  } else if (preg_match("/J-PHONE[\s\/]\d\.\d//", $ua_string, $matches)) {
    $bws = "J-PHONE";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/SoftBank[\s\/]\d\.\d//", $ua_string, $matches)) {
    $bws = "SoftBank";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  } else if (preg_match("/Mozilla[\s\/]\d\.\d/", $ua_string, $matches)) {
    $bws = "Mozilla";
    $bwsver = substr($matches[0], strlen($bws) + 1, 3);
  }
  
  // OS判定
  if (stristr($ua_string, "Linux") !== false) {
    $os = "Linux";
  } else if (stristr($ua_string, "BSD") !== false) {
    if (stristr($ua_string, "FreeBSD") !== false) {
      $os = "FreeBSD";
    } else {
      $os = "BSD";
    }
  } else if (stristr($ua_string, "SunOS") !== false) {
    $os = "SunOS";
    if (preg_match("/SunOS[\s\/]\d\.\d/", $ua_string, $matches)) {
      $osver = substr($matches[0], strlen($os) + 1, 3);
    }
    $os = "Solaris";
  } else if (preg_match("/Win[dN9][oT58]/", $ua_string, $matches)) {
    $os = "Windows";
    if (stristr($ua_string, "Windows NT 5.0") !== false) {
      $osver = "2000";
    } else if (stristr($ua_string, "Windows 2000") !== false) {
      $osver = "2000";
    } else if (stristr($ua_string, "Windows NT 5.1") !== false) {
      $osver = "XP";
    } else if (stristr($ua_string, "Windows NT 5.2") !== false) {
      $osver = "2003";
    } else if (stristr($ua_string, "Windows XP") !== false) {
      $osver = "XP";
    } else if (stristr($ua_string, "Windows NT 6.0") !== false) {
      $osver = "Vista";
    } else if (stristr($ua_string, "Windows NT 6.1") !== false) {
      $osver = "7";
    } else if (stristr($ua_string, "Windows 98") !== false) {
      $osver = "98";
    } else if (stristr($ua_string, "Windows 95") !== false) {
      $osver = "95";
    } else if (stristr($ua_string, "Windows Me") !== false) {
      $osver = "Me";
    } else if (stristr($ua_string, "Windows NT") !== false) {
      $osver = "NT";
    } else if (stristr($ua_string, "Win95") !== false) {
      $osver = "95";
    } else if (stristr($ua_string, "Win98") !== false) {
      $osver = "98";
    } else if (stristr($ua_string, "WinNT") !== false) {
      $osver = "NT";
    }
  } else if (stristr($ua_string, "Mac") !== false) {
    if (stristr($ua_string, "iPod") !== false) {
      $os = "iPod";
    } else if (stristr($ua_string, "iPhone") !== false) {
      $os = "iPhone";
    } else if (stristr($ua_string, "iPad") !== false) {
      $os = "iPad";
    } else if (stristr($ua_string, "Mac OS X") !== false) {
      $os = "MacOS";
      $osver = "X";
    } else {
      $os = "MacOS";
    }
  } else if (stristr($ua_string, "Nintendo") !== false) {
    $os = "Nintendo";
  } else if (stristr($ua_string, "PlayStation") !== false) {
    $os = "PlayStation";
  } else if (stristr($ua_string, "DreamPassport") !== false) {
    $os = "DreamPassport";
  }
  
  // 返却値
  return array($bws, $bwsver, $os, $osver);
}

トラックバック

このエントリーのトラックバックURL:
http://www.soundcanvas.jp/blog/mt-ping.cgi/21

コメントを投稿

About

2010年2月14日 09:35に投稿されたエントリのページです。

ひとつ前の投稿は「VisualSVNを入れてみた」です。

次の投稿は「アクセスログは非同期で取るべきか」です。

他にも多くのエントリがあります。メインページアーカイブページも見てください。

Powered by
Movable Type