PHP & Others

방문자의 platform,os,language,browser name, version

페이지 정보

본문

정규표현식 이용

<?php
      function _get_os_info () {
              $USER_AGENT    = $_SERVER['HTTP_USER_AGENT'];
              $regex_windows  = '/\\s(?:(?!dar))win(?:dows)?\\s?([0-9a-z]*)[\\w\\s]?([a-z0-9.]*)/i';
              $regex_mac      = '/(68[k0]{1,3})|(ppc mac os x)|([p\\S]{1,5}pc)|(darwin)/i';
              $regex_os2      = '/os\\/2|ibm-webexplorer/i';
              $regex_sunos    = '/(sun|i86)[os\\s]*([0-9]*)/i';
              $regex_irix    = '/(irix)[\\s]*([0-9]*)/i';
              $regex_hpux    = '/(hp-ux)[\\s]*([0-9]*)/i';
              $regex_aix      = '/aix([0-9]*)/i';
              $regex_dec      = '/dec|osfl|alphaserver|ultrix|alphastation/i';
              $regex_vms      = '/vax|openvms/i';
              $regex_sco      = '/sco|unix_sv/i';
              $regex_linux    = '/x11|inux/i';
              $regex_bsd      = '/(free)?(bsd)/i';
              $regex_amiga    = '/amiga[os]?/i';


              $_browsers = array(
                    'microsoft internet explorer' => 'IE', 'msie' => 'IE', 'netscape6' => 'NS', 'netscape' => 'NS', 'galeon' => 'GA', 'phoenix' => 'PX', 'mozilla firebird' => 'FB', 'firebird' => 'FB', 'chimera' => 'CH', 'camino' => 'CA', 'safari' => 'SF', 'k-meleon' => 'KM', 'mozilla' => 'MZ', 'opera' => 'OP', 'konqueror' => 'KQ', 'icab' => 'IC', 'lynx' => 'LX', 'links' => 'LI', 'ncsa mosaic' => 'MO', 'amaya' => 'AM', 'omniweb' => 'OW', 'hotjava' => 'HJ', 'browsex' => 'BX', 'amigavoyager' => 'AV', 'amiga-aweb' => 'AW', 'ibrowse' => 'IB', 'microsoft url control' => 'IU', 'Windows-Media-Player' => 'WP', 'scooter' => 'SC', 'NSPlayer' => 'ns', 'IP\\*Works!' => 'IW', 'WebCopier' => 'WC', 'Wget' => 'WG'
              );

             
              if(preg_match_all($regex_windows,$USER_AGENT,$match)) {
                    $v  = $match[1][count($match[0])-1];
                    $v2 = $match[2][count($match[0])-1];

                            if(stristr($v,'NT') && $v2 == 5.1) $v = 'xp';
                            elseif(stristr($v,'NT') && $v2 == 5.0) $v = '2000';
                            elseif(stristr($v,'NT') && $v2 == 5.2) $v = '2003';
                            elseif($v == '2000') $v = '2000';
                            elseif(stristr($v,'9x') && $v2 == 4.9) $v = '98';
                            elseif($v.$v2 == '16bit') $v = '3.1';
                            else $v .= $v2;

                    if(empty($v)) $v = 'win';
                    $os =  strtolower($v);
                    $platform =  'win';
              }
             
              elseif(preg_match($regex_amiga,$USER_AGENT,$match)) {
                    $platform =  'amiga';
                    if(stristr($USER_AGENT,'morphos')) {
                            $os =  'morphos';
                    } elseif(stristr($USER_AGENT,'mc680x0')) {
                            $os =  'mc680x0';
                    } elseif(stristr($USER_AGENT,'ppc')) {
                            $os =  'Mac PowerPC';
                    } elseif(preg_match('/(AmigaOS [\\.1-9]?)/i',$USER_AGENT,$match)) {
                            $os =  $match[1];
                    }
              }
             
              elseif(preg_match($regex_os2,$USER_AGENT)) {
                    $os =  'os2';
                    $platform =  'os2';
              }

              elseif( preg_match($regex_mac,$USER_AGENT,$match) ) {
                $platform =  'mac';
                    $os = !empty($match[1]) ? '68k' : '';
                    $os = !empty($match[2]) ? 'os X' : $os;
                    $os = !empty($match[3]) ? 'Mac PowerPC' : $os;
                    $os = !empty($match[4]) ? 'os X' : $os;
                    $os =  $os;
              }

              elseif(preg_match($regex_sunos,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    if(!stristr('sun',$match[1])) $match[1] = 'sun'.$match[1];
                    $os =  $match[1].$match[2];
              }

              elseif(preg_match($regex_irix,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $os =  $match[1].$match[2];
              }

              elseif(preg_match($regex_hpux,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $match[1] = str_replace('-','',$match[1]);
                    $match[2] = (int) $match[2];
                    $os =  $match[1].$match[2];
              }

              elseif(preg_match($regex_aix,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $os =  'aix'.$match[1];
              }

              elseif(preg_match($regex_dec,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $os =  'dec';
              }

              elseif(preg_match($regex_vms,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $os =  'vms';
              }

              elseif(preg_match($regex_sco,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $os =  'sco';
              }

              elseif(stristr($USER_AGENT,'unix_system_v')) {
                    $platform =  '*nix';
                    $os =  'unixware';
              }

              elseif(stristr($USER_AGENT,'ncr')) {
                    $platform =  '*nix';
                    $os =  'mpras';
              }

              elseif(stristr($USER_AGENT,'reliantunix')) {
                    $platform =  '*nix';
                    $os =  'reliant';
              }

              elseif(stristr($USER_AGENT,'sinix')) {
                    $platform =  '*nix';
                    $os =  'sinix';
              }

              elseif(preg_match($regex_bsd,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $os =  $match[1].$match[2];
              }

              elseif(preg_match($regex_linux,$USER_AGENT,$match)) {
                    $platform =  '*nix';
                    $os =  'linux';
              }

              else {
                    $platform = $os = "order";
              }


              $browsers = '';
              while(list($k,) = each($_browsers))
              {      if(!empty($browsers)) $browsers .= "|";
                    $browsers .= $k;
              }
              $version_string = "[\\/\\sa-z(]*([0-9]+)([\\.0-9a-z]+)?";
              if(preg_match_all("/($browsers)$version_string/i",$USER_AGENT,$results))
              {     
                    $count = count($results[0])-1;

                    $browser = $results[1][$count];
                    $long_name=$results[1][$count];
                    $maj_ver= $results[2][$count];

                    if(isset($results[3][$count])) {
                            $min_ver=$results[3][$count];
                    } else {
                            $min_ver='.0';
                    }

                    $version = $maj_ver.$min_ver;
              }

              $language = preg_replace('/;q=\\d.\\d+|,?rs\\d_[a-z\\d]+/i', '', $_SERVER["HTTP_ACCEPT_LANGUAGE"]      );

              if(!$language) $language = "order";

              return array("platform"=>$platform,"os"=>$os,"language"=>$language,"version"=>$version,"long_name"=>$long_name);
      }
      $agent = _get_os_info();
      echo "platform = ".$agent['platform'];
      echo "<P>";
      echo "os = ".$agent['os'];
      echo "<P>";
      echo "language = ".$agent['language'];
      echo "<P>";
      echo "version = ".$agent['version'];
      echo "<P>";
      echo "long_name = ".$agent['long_name'];

?>

관련자료

등록된 댓글이 없습니다.
Today's proverb
가루는 칠수록 고와지고 말은 할수록 거칠어진다.