How PHP obtains the client browser name and version (based on the Ecshop function) _php tips

Source: Internet
Author: User
Tags php programming

This example describes how PHP obtains the client browser name and version. Share to everyone for your reference, specific as follows:

See a function in Ecshop get_user_browser () to get the name and version of the browser. Although the information obtained is simply some information, it is still very useful. The principle is mainly through the $_server[' http_user_agent '] to obtain browser information, and then use the comparison to get the browser information.

The following are the effects of each browser run:

The source code is as follows:

<?php function Get_user_browser () {if (Empty ($_server[' http_user_agent '))) {return ';
  } $agent = $_server[' http_user_agent '];
  $browser = ';
  $browser _ver = '; if (preg_match) ('/msie\s ([^\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 ('/netcaptor\s) ([^\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 ';

 } echo Get_user_browser ();?>

I hope this article will help you with your PHP programming.

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.