PHP code for determining browser types

Source: Internet
Author: User
In php, to determine the browser type, 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 expressions

In php, if you want to determine the browser type, 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 expressions to determine the type or browser version.

PHP determines the browser type and language, because when the browser connects to the server, it will first send some content containing its own information (browser type and language ).

Here we mainly analyze _ SERVER ["HTTP_USER_AGENT"] (browser type) and _ SERVER ["HTTP_ACCEPT_LANGUAGE"] (browser language ). all we need to do is read the content and use the strpos or preg_match function for comparison.

The code for determining the browser type instance is as follows:

  1.  

The PHP code section is provided first. some of them are not very complete. you need to add the PHP code as needed. (the following code has a small error. please read the article and modify it yourself)

The instance code is as follows:

  1. If (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 9.0 "))
  2. Echo & quot; Internet Explorer 9.0 & quot ";
  3. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 8.0 "))
  4. Echo & quot; Internet Explorer 8.0 & quot ";
  5. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 7.0 "))
  6. Echo & quot; Internet Explorer 7.0 & quot ";
  7. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 6.0 "))
  8. Echo & quot; Internet Explorer 6.0 & quot ";
  9. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Firefox "))
  10. Echo "Firefox ";
  11. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Chrome "))
  12. Echo "Chrome ";
  13. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Safari "))
  14. Echo "Safari ";
  15. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Opera "))
  16. Echo "Opera ";
  17. Else echo $ _ SERVER ["HTTP_USER_AGENT"];
  18. ?>

Open operabrowser and you will see the following page request header information:

Opera/9.80 (Windows NT 5.1; U; Edition IBIS; zh-cn) Presto/2.10.229 Version/11.61

However, the value returned by strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Opera") is always "0"

The solution is relatively simple. the instance code is as follows:

  1. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Opera "))
  2. Replace
  3. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "pera "))

Next we will add a stronger one to determine whether it is a browser user or seo/seo.html "target =" _ blank "> Search Engine

The instance code is as follows:

  1. Function my_get_browser (){
  2. If (emptyempty ($ _ SERVER ['http _ USER_AGENT ']) {
  3. Return 'command line. The robot is here! ';
  4. }
  5. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'msie 9.0 ')){
  6. Return 'Internet Explorer 9.0 ';
  7. }
  8. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'msie 8.0 ')){
  9. Return 'Internet Explorer 8.0 ';
  10. }
  11. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'msie 7.0 ')){
  12. Return 'Internet Explorer 7.0 ';
  13. }
  14. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'msie 6.0 ')){
  15. Return 'Internet Explorer 6.0 ';
  16. }
  17. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'Firefox ')){
  18. Return 'Firefox ';
  19. }
  20. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'Chrome ')){
  21. Return 'Chrome ';
  22. }
  23. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'Safari ')){
  24. Return 'Safari ';
  25. }
  26. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], 'Opera ')){
  27. Return 'Opera ';
  28. }
  29. If (false! = Strpos ($ _ SERVER ['http _ USER_AGENT '], '360se ')){
  30. Return '360se ';
  31. }
  32. }

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.