Home  >  Article  >  Backend Development  >  How to get the client browser name and version in php (based on ECShop function), _PHP tutorial

How to get the client browser name and version in php (based on ECShop function), _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:39989browse

How to get the name and version of the client browser in php (based on ECShop function),

The example in this article describes how php gets the name and version of the client browser. Share it with everyone for your reference, the details are as follows:

I saw that there is such a function get_user_browser() in ecshop to get the name and version of the browser. Although the information obtained is only simple information, it is still very practical. The principle is to obtain browser information through $_SERVER['HTTP_USER_AGENT'], and then use regular rules to compare and obtain the browser information.

The following is the effect of each browser:

The source code is as follows:

<&#63;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();
&#63;>

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • How to get the user browser version in PHP
  • Two methods of PHP to get the browser information class and client location
  • PHP code to obtain the user’s browser and operating system information
  • PHP judgment browser, judgment language code sharing
  • PHP class to implement reading mobile client browser
  • Summary of 4 functions of PHP outputting content to the browser
  • How to prevent the browser from using cached pages in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084556.htmlTechArticleHow to obtain the name and version of the client browser in php (based on the ECShop function). The example in this article describes how php obtains customers browser name and version. Sharing it with everyone for your reference,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn