Detailed explanation of PHP browser type determination program code_PHP tutorial

WBOY
Release: 2016-07-13 17:15:58
Original
962 people have browsed it

If we want to determine the browser type in php, the operation method is very simple. We only need to use the global variable HTTP_USER_AGENT to obtain the user's browser information, so that we can use regular rules to determine the type or browser version.

How does PHP determine the browser type and browser language? Because the browser will first send some content containing its own information (browser type, language) when connecting to the server.

Here we mainly analyze _SERVER["HTTP_USER_AGENT"] (browser type) and _SERVER["HTTP_ACCEPT_LANGUAGE"] (browser language).
All we have to do is read these contents and compare them using strpos or preg_match function.
Determine browser type:

The code is as follows Copy code
 代码如下 复制代码

 

The PHP code part is given first. Some of it is not complete. Friends who need it can add it by themselves. (There is a small error in the code below, please read the article and modify it yourself)
 代码如下 复制代码

if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 9.0"))
echo "Internet Explorer 9.0";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 8.0"))
echo "Internet Explorer 8.0";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 7.0"))
echo "Internet Explorer 7.0";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 6.0"))
echo "Internet Explorer 6.0";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox"))
echo "Firefox";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Chrome"))
echo "Chrome";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Safari"))
echo "Safari";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera"))
echo "Opera";
else echo $_SERVER["HTTP_USER_AGENT"];
?>

The code is as follows Copy code

if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 9.0"))

echo "Internet Explorer 9.0";
else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 8.0"))

echo "Internet Explorer 8.0";

else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 7.0"))

echo "Internet Explorer 7.0";
代码如下 复制代码

else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera"))

替换成

else if(strpos($_SERVER["HTTP_USER_AGENT"],"pera"))

else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 6.0"))<🎜> echo "Internet Explorer 6.0";<🎜> else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox"))<🎜> echo "Firefox";<🎜> else if(strpos($_SERVER["HTTP_USER_AGENT"],"Chrome"))<🎜> echo "Chrome";<🎜> else if(strpos($_SERVER["HTTP_USER_AGENT"],"Safari"))<🎜> echo "Safari";<🎜> else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera"))<🎜> echo "Opera";<🎜> else echo $_SERVER["HTTP_USER_AGENT"];<🎜> ?>

Open the opera browser and you can see its page request header information as follows: Opera/9.80 (Windows NT 5.1; U; Edition IBIS; zh-cn) Presto/2.10.229 Version/11.61 But the value returned by strpos($_SERVER["HTTP_USER_AGENT"],"Opera") is always "0" The solution is relatively simple,
The code is as follows Copy code
else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera")) Replace with else if(strpos($_SERVER["HTTP_USER_AGENT"],"pera"))

The following is a stronger method to determine whether it is a browser user or seo/seo.html" target="_blank">search engine

The code is as follows
 代码如下 复制代码


function my_get_browser(){
 if(empty($_SERVER['HTTP_USER_AGENT'])){
  return '命令行,机器人来了!';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
  return 'Internet Explorer 9.0';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
  return 'Internet Explorer 8.0';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
  return 'Internet Explorer 7.0';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
  return 'Internet Explorer 6.0';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
  return 'Firefox';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
  return 'Chrome';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
  return 'Safari';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
  return 'Opera';
 }
 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
  return '360SE';
 }
}

Copy code

function my_get_browser(){
if(empty($_SERVER['HTTP_USER_AGENT'])){
Return 'Command line, the robot is coming! ';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
Return 'Internet Explorer 9.0';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
Return 'Internet Explorer 8.0';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
Return 'Internet Explorer 7.0';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
Return 'Internet Explorer 6.0';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
return 'Firefox';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
return 'Chrome';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
return 'Safari';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
return 'Opera';
}
if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
return '360SE';
}
}

http://www.bkjia.com/PHPjc/628724.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/628724.html
TechArticle
If you want to determine the browser type in php, the operation method is very simple, we only need to use the global variable HTTP_USER_AGENT to obtain it User browser information so we can take advantage of regular...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!