Home > Backend Development > PHP Tutorial > php gets the browser type of the visitor browsing the page

php gets the browser type of the visitor browsing the page

高洛峰
Release: 2023-03-05 11:34:01
Original
1396 people have browsed it

The method is as follows

Check the user's agent string, which is part of the HTTP request sent by the browser. Use $_SERVER['HTTP_USER_AGENT'] to get the agent string information.

For example:

<?php
 echo $_SERVER[&#39;HTTP_USER_AGENT&#39;];
?>
Copy after login

It may be printed like this:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Copy after login

Encapsulated into a function:

function my_get_browser(){
 if(empty($_SERVER[&#39;HTTP_USER_AGENT&#39;])){
  return &#39;robot!&#39;;
 }
 if( (false == strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;MSIE&#39;)) && (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;Trident&#39;)!==FALSE) ){
  return &#39;Internet Explorer 11.0&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;MSIE 10.0&#39;)){
  return &#39;Internet Explorer 10.0&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;MSIE 9.0&#39;)){
  return &#39;Internet Explorer 9.0&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;MSIE 8.0&#39;)){
  return &#39;Internet Explorer 8.0&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;MSIE 7.0&#39;)){
  return &#39;Internet Explorer 7.0&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;MSIE 6.0&#39;)){
  return &#39;Internet Explorer 6.0&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;Edge&#39;)){
  return &#39;Edge&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;Firefox&#39;)){
  return &#39;Firefox&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;Chrome&#39;)){
  return &#39;Chrome&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;Safari&#39;)){
  return &#39;Safari&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;Opera&#39;)){
  return &#39;Opera&#39;;
 }
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;360SE&#39;)){
  return &#39;360SE&#39;;
 }
  //微信浏览器
 if(false!==strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&#39;MicroMessage&#39;)){
  return &#39;MicroMessage&#39;;
 }>
}
Copy after login

Summary

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. If If you have any questions, you can leave a message to communicate.

For more articles related to PHP obtaining the browser type of the visitor's browsing page, please pay attention to the PHP Chinese website!

Related labels:
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