PHP get Browser name version Instance program _php Tutorial

Source: Internet
Author: User
Tags escape quotes
Get the browser version in PHP directly using http_user_agent can be, but to determine the specific browser, what version we need to judge each,

Example 1

The code is as follows Copy Code

function Get_user_browser () {
if (Empty ($_server[' http_user_agent ')) {
Return ';
}

$agent = $_server[' http_user_agent ');
$browser = ";
$browser _ver = ";

if (Preg_match ('/msies ([^s|;] +)/I ', $agent, $regs)) {
$browser = ' Internet Explorer ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/firefox/([^s]+)/I ', $agent, $regs)) {
$browser = ' FireFox ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/maxthon/i ', $agent, $regs)) {
$browser = ' (Internet Explorer '. $browser _ver. ') Maxthon ';
$browser _ver = ";
} elseif (Preg_match ('/opera[s|/] ([^s]+)/I ', $agent, $regs)) {
$browser = ' Opera ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/omniweb/(v*) ([^s|;] +)/I ', $agent, $regs)) {
$browser = ' OmniWeb ';
$browser _ver = $regs [2];
} elseif (Preg_match ('/netscape ([d]*)/([^s]+)/I ', $agent, $regs)) {
$browser = ' Netscape ';
$browser _ver = $regs [2];
} elseif (Preg_match ('/safari/([^s]+)/I ', $agent, $regs)) {
$browser = ' Safari ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/netcaptors ([^s|;] +)/I ', $agent, $regs)) {
$browser = ' (Internet Explorer '. $browser _ver. ') Netcaptor ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/lynx/([^s]+)/I ', $agent, $regs)) {
$browser = ' Lynx ';
$browser _ver = $regs [1];
}

if (!empty ($browser)) {
Return Addslashes ($browser. ' ' . $browser _ver);
} else {
Return ' Unknow browser ';
}
}

Instance

The code is as follows Copy Code

function Get_user_browser ()
{
if (Empty ($_server[' http_user_agent '))
{//When the browser does not send the visitor's information
Return ';
}

$agent = $_server[' http_user_agent ');
$browser = ";
$browser _ver = ";

if (Preg_match ('/msies ([^s|;] +)/I ', $agent, $regs))
{
$browser = ' Internet Explorer '; When the match to the MSIE, the acquisition of the number of the room in the array $regs
$browser _ver = $regs [1];
}
ElseIf (Preg_match ('/firefox/([^s]+)/I ', $agent, $regs))
{
$browser = ' FireFox '; When matching to firefox/, get the number part that follows
$browser _ver = $regs [1];
}
ElseIf (Preg_match ('/maxthon/i ', $agent, $regs))
{
$browser = ' (Internet Explorer '. $browser _ver. ') Maxthon ';
$browser _ver = ";
}
ElseIf (Preg_match ('/opera[s|/] ([^s]+)/I ', $agent, $regs))
{
$browser = ' Opera ';
$browser _ver = $regs [1];
}
ElseIf (Preg_match ('/omniweb/(v*) ([^s|;] +)/I ', $agent, $regs))
{
$browser = ' OmniWeb ';
$browser _ver = $regs [2];
}
ElseIf (Preg_match ('/netscape ([d]*)/([^s]+)/I ', $agent, $regs))
{
$browser = ' Netscape ';
$browser _ver = $regs [2];
}
ElseIf (Preg_match ('/safari/([^s]+)/I ', $agent, $regs))
{
$browser = ' Safari ';
$browser _ver = $regs [1];
}
ElseIf (Preg_match ('/netcaptors [^s|;] +)/I ', $agent, $regs))
{
$browser = ' (Internet Explorer '. $browser _ver. ') Netcaptor ';
$browser _ver = $regs [1];
}
ElseIf (Preg_match ('/lynx/([^s]+)/I ', $agent, $regs))
{
$browser = ' Lynx ';
$browser _ver = $regs [1];
}

if (!empty ($browser))
{
Return Addslashes ($browser. ' ' . $browser _ver);//Escape Quotes
}
Else
{
Return ' Unknow browser ';
}
}


Judging whether it's a spider

function Is_spider ($record = true)//Judging whether it is a spider
{
static $spider = NULL;

if ($spider!== NULL)
{
return $spider; //
}

if (Empty ($_server[' http_user_agent '))
{
$spider = ";

Return ';
}

$searchengine _bot = Array (
' Googlebot ',
' Mediapartners-google ',
' Baiduspider+ ',
' MSNBot ',
' Yodaobot ',
' Yahoo! slurp; ',
' Yahoo! Slurp China; ',
' Iaskspider ',
' Sogou web Spider ',
' Sogou push spider '
);

$searchengine _name = Array (
' GOOGLE ',
' GOOGLE ADSENSE ',
' BAIDU ',
' MSN ',
' Yodao ',
' YAHOO ',
' Yahoo China ',
' Iask ',
' SOGOU ',
' SOGOU '
);

$spider = Strtolower ($_server[' http_user_agent '); Turn user browser information into lowercase

foreach ($searchengine _bot as $key = $value)
{
if (Strpos ($spider, $value)!== false)//absolute not equal to avoid the case where the string appears to be 0
{
$spider = $searchengine _name[$key]; Returns the corresponding seo/seo.html "target=" _blank "> Search engine Name

return $spider;
}
}

$spider = ";

Return ';
}

http://www.bkjia.com/PHPjc/631523.html www.bkjia.com true http://www.bkjia.com/PHPjc/631523.html techarticle get the browser version in PHP directly using Http_user_agent, but to determine the specific browser, what version we need to judge each, example 1 code as follows ...

  • 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.