PHP judges the browser type program code details. 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, in this way, we can use regular expressions in php. to determine the browser type, we only need to use the global variable HTTP_USER_AGENT to obtain the user's browser information, in this way, 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.
Determine the browser type:
The PHP code section is provided first. some of them are not very complete. you need to add them by yourself based on the Huludao. (The following code has a small error. please read the article and modify it on your own)
| The code is as follows: |
|
|
If (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 9.0 "))
Echo & quot; Internet Explorer 9.0 & quot ";
Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 8.0 "))
Echo & quot; Internet Explorer 8.0 & quot ";
Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 7.0 "))
Echo & quot; Internet Explorer 7.0 & quot ";
Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 6.0 "))
Echo & quot; Internet Explorer 6.0 & quot ";
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 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 code is as follows: |
|
|
Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Opera "))
Replace
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 code is as follows: |
|
|
Function my_get_browser (){
If (empty ($ _ SERVER ['http _ USER_AGENT ']) {
Return 'command line. The robot is here! ';
}
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 ';
}
}
|
...