Home >Backend Development >PHP Problem >How to determine if php is a mobile browser

How to determine if php is a mobile browser

王林
王林Original
2021-06-18 14:06:172561browse

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. .

How to determine if php is a mobile browser

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.

How to determine if php is a mobile browser

Let’s output this string first and see what the data looks like.

How to determine if php is a mobile browser

If you open it on an Android phone, there will be android characters.

How to determine if php is a mobile browser

#When opened on an Apple phone, there will be iphone characters.

How to determine if php is a mobile browser

But when you open it from the computer, there are no characters above.

How to determine if php is a mobile browser

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.

How to determine if php is a mobile browser

#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.

How to determine if php is a mobile browser

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!

Statement:
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