PHP detection useragent Version Example _php Tutorial

Source: Internet
Author: User
Tags php define
Copy CodeThe code is as follows:
Define (' IE ', 1);
Define (' FIREFOX ', 2);
Define (' CHROME ', 3);
Define (' OPERA ', 4);
Class Useragentdetect
{
Static $version = Array ();

static $userAgent = ';

static function Getuseragent ()
{
$header = Getallheaders ();
Self:: $userAgent = $header [' user-agent '];
}
function Finduseragentbykey ($ua, $key)
{
$len = strlen ($key);
$start = Strpos ($ua, $key);

$pos = Strpos ($ua, ", $start + $len + 1);

Return substr ($ua, $start + $len, $pos-$len-$start);
}
static function Detectuseragent ()
{
Self:: Getuseragent ();

if (Strpos (self:: $userAgent, ' MSIE ')! = null)
{
Self:: $version [] = IE;
Self:: $version [] =self:: Finduseragentbykey (self:: $userAgent, ' MSIE ');
}
else if (Strpos (self:: $userAgent, ' gecko/')! = null)
{

Self:: $version [] = FIREFOX;
Self:: $version [] =self:: Finduseragentbykey (self:: $userAgent, ' firefox/');
}
else if (Strpos (self:: $userAgent, ' applewebkit/')! = null)
{
Self:: $version [] = CHROME;
Self:: $version [] = self:: Finduseragentbykey (self:: $userAgent, ' chrome/');
}
else if (Strpos (self:: $userAgent, ' presto/')! = null)
{
Self:: $version [] = OPERA;
Self:: $version [] = self:: Finduseragentbykey (self:: $userAgent, ' opera/');
}
Else
{
}

Return self:: $version;
}


function Matchuseragent ($ua, $key)
{
Preg_match ("/$key. ( [^\s]*?] \s/", $ua, $out);

return $out [1];
}
}
$ua =useragentdetect::d etectuseragent ();

Print_r ($ua);

http://www.bkjia.com/PHPjc/745210.html www.bkjia.com true http://www.bkjia.com/PHPjc/745210.html techarticle Copy the code as follows: PHP define (' IE ', 1); define (' FIREFOX ', 2); define (' CHROME ', 3); define (' OPERA ', 4); Class Useragentdetect {Static $version = array (); static $userAgent ...

  • Related Article

    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.