PHP Test useragent Version sample

Source: Internet
Author: User
Tags php define

  This article mainly introduces the PHP detection useragent version of the example, the need for friends can refer to the following

    Code as follows: <?php define (' IE ', 1); Define (' FIREFOX ', 2); Define (' CHROME ', 3); Define (' OPERA ', 4); Class Useragentdetect {    static $version = Array ()       static $userAgent = ';   &nbsp ;   static function getuseragent ()    :         $header = Getallheaders ();         Self:: $userAgent = $header [' user-agent '];    }    function Finduseragentbykey ($ua, $key)     {        $len = str Len ($key);         $start = Strpos ($ua, $key);           $pos = Strpos ($ua, ', $start + $len + 1);           return substr ($ua, $start + $len, $pos-$len-$start);    }      static function detectuseragent ()     {         se LF:: Getuseragent ();           if (Strpos (self:: $userAgent, ' MSIE ')!= null)         {            self:: $version [] = IE;       &NB Sp     Self:: $version [] =self:: Finduseragentbykey (self:: $userAgent, ' msie ');        }          ELSE if (Strpos (self:: $userAgent, ' gecko/')!= null &NB Sp       {     ,         self:: $version [] = FIREFOX             Self:: $version [] =self::  finduseragentbykey (self:: $userAgent, ' firefox/');        }          ELSE if Strpos (self:: $userAgent, ' applewebkit/')!= nul L)         {            self:: $version [] = CHROME;     &NBS P       Self:: $version [] = self:: Finduseragentbykey (self:: $userAgent, ' chrome/');        }          ELSE if (Strpos (selF:: $userAgent, ' presto/')!= null)         {            self:: $vers Ion[] = OPERA;             self:: $version [] = self:: Finduseragentbykey (self:: $userAgent, ' opera/');        }          else         {      &N Bsp }            return self:: $version;    }          function matchuseragent ($ua, $key)     {        Preg_match ("/$key. ( [^s]*?] S/", $ua, $out);           return $out [1];    } }  $ua =useragentdetect::d etectuseragent ();   Print_r ($ua);     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.