Home>Article>Backend Development> How to determine if php is a mobile browser
The way PHP determines whether it is a mobile browser is to use the HTTP_USER_AGENT variable under the _Server object to obtain the browser agent used to open the page, and output the browser agent string to view the output results. .
The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.
When using php to develop pages, we sometimes need to determine whether the web page is opened from a mobile phone or a computer. So how should we judge when we encounter such a problem? Let’s take a look at it together.
First we can use the HTTP_USER_AGENT variable under the _Server object to obtain the browser agent of the user opening the page.
Let’s output this string first and see what the data looks like.
If you open it on an Android phone, there will be android characters.
#When opened on an Apple phone, there will be iphone characters.
But when you open it from the computer, there are no characters above.
So we modify the code and determine the agent of the user's browser to see if it contains android or iphone characters. If it does, it is the mobile version.
#Modify the code running page and open it on the mobile phone. It can be correctly judged that it is opened on the mobile phone.
Of course, in addition to these two systems, some old mobile phones also use other systems. Although there are fewer of them, for better compatibility, we add the system characters of these mobile phones in the if condition judgment. Just skewer it.
Related recommendations:php video tutorial
The above is the detailed content of How to determine if php is a mobile browser. For more information, please follow other related articles on the PHP Chinese website!