Home  >  Article  >  Backend Development  >  Function of get_user_browser() function in PHP ECShop

Function of get_user_browser() function in PHP ECShop

墨辰丷
墨辰丷Original
2018-06-05 16:22:121457browse

This article mainly introduces the function of get_user_browser() function in PHP ECShop. Interested friends can refer to it. I hope it will be helpful to everyone.

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 :

<?php
function get_user_browser()
{
  if (empty($_SERVER[&#39;HTTP_USER_AGENT&#39;]))
  {
    return &#39;&#39;;
  }
  $agent  = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
  $browser  = &#39;&#39;;
  $browser_ver = &#39;&#39;;
  if (preg_match(&#39;/MSIE\s([^\s|;]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;Internet Explorer&#39;;
    $browser_ver = $regs[1];
  }
  elseif (preg_match(&#39;/FireFox\/([^\s]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;FireFox&#39;;
    $browser_ver = $regs[1];
  }
  elseif (preg_match(&#39;/Maxthon/i&#39;, $agent, $regs))
  {
    $browser  = &#39;(Internet Explorer &#39; .$browser_ver. &#39;) Maxthon&#39;;
    $browser_ver = &#39;&#39;;
  }
  elseif (preg_match(&#39;/Opera[\s|\/]([^\s]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;Opera&#39;;
    $browser_ver = $regs[1];
  }
  elseif (preg_match(&#39;/OmniWeb\/(v*)([^\s|;]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;OmniWeb&#39;;
    $browser_ver = $regs[2];
  }
  elseif (preg_match(&#39;/Netscape([\d]*)\/([^\s]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;Netscape&#39;;
    $browser_ver = $regs[2];
  }
  elseif (preg_match(&#39;/safari\/([^\s]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;Safari&#39;;
    $browser_ver = $regs[1];
  }
  elseif (preg_match(&#39;/NetCaptor\s([^\s|;]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;(Internet Explorer &#39; .$browser_ver. &#39;) NetCaptor&#39;;
    $browser_ver = $regs[1];
  }
  elseif (preg_match(&#39;/Lynx\/([^\s]+)/i&#39;, $agent, $regs))
  {
    $browser  = &#39;Lynx&#39;;
    $browser_ver = $regs[1];
  }
  if (!empty($browser))
  {
    return addslashes($browser . &#39; &#39; . $browser_ver);
  }
  else
  {
    return &#39;Unknow browser&#39;;
  }
}
echo get_user_browser();
?>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Sharing examples of two methods of converting PHP array encoding gbk and utf8 to each other

PHP implements replacement for multiple users Avatar function example sharing

PHP method to detect whether the URL can be opened normally

The above is the detailed content of Function of get_user_browser() function in PHP ECShop. For more information, please follow other related articles on the PHP Chinese website!

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