In today's mobile Internet era, optimizing website design to adapt to the use of different user terminal devices has become a necessary task. Therefore, how to determine in PHP whether a user is using a mobile phone or a PC when accessing a website has become one of the problems that developers need to face. This article will introduce some common PHP codes to help developers determine whether the client opening the website is a mobile phone or PC.
1. Determine the access terminal through HTTP_USER_AGENT
HTTP_USER_AGENT is a header information in the HTTP protocol. The request header contains a lot of device identification information, which can be used to determine the access terminal device. type. By analyzing the HTTP_USER_AGENT in the user request header, the browser, operating system, mobile device and other information used by the user can be obtained, which also includes the identification information of the mobile device or PC device.
The specific implementation is as follows:
function is_mobile(){ if (isset($_SERVER['HTTP_USER_AGENT'])) { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_agents = Array("Android", "iPhone", "iPod", "iPad", "Windows Phone", "BlackBerry", "SymbianOS"); foreach ($mobile_agents as $mobile_agent) { if (strpos($user_agent, $mobile_agent) !== false) { return true; } } } return false; }
Iterate over the $mobile_agents array, and you can determine whether the access terminal is a mobile device based on whether HTTP_USER_AGENT contains the mobile device identifier. If true is returned, it means that the user used a mobile device, otherwise it means that the user used a PC device.
2. Determine the access terminal through the suspected original method of $_SERVER
In addition to the HTTP_USER_AGENT header information, there is another judgment method, that is, judging by the HTTP_ACCEPT value in the suspected original method of $_SERVER Access terminal type. The HTTP_ACCEPT value represents the page type that the user's browser can accept. Usually the browser accepts text/html type data by default, while mobile devices request text/html type or wap type data format.
The specific implementation code is as follows:
function is_mobile(){ if (isset($_SERVER['HTTP_ACCEPT'])) { $accept = $_SERVER['HTTP_ACCEPT']; if (strpos($accept, 'vnd.wap.xhtml+xml') !== false || strpos($accept, 'text/vnd.wap.wml') !== false) { return true; } } return false; }
If true is returned, it means that the user used a mobile device to access, otherwise it means that the user used a PC device to access.
3. Access specific URL paths through mobile devices
By default, when a mobile device accesses a website, it will automatically jump to the mobile version of the webpage, while when a desktop device accesses it, it will jump Go to the desktop version of the page. Developers can handle requests from different devices based on differences in URL paths.
For example, adding the "/m/" string to the URL means that the page being accessed is a mobile website. After adapting the content, it can still be rendered. If the access URL does not have the "/m/" string, it means that you are accessing the PC page.
The specific implementation code is as follows:
function is_mobile(){ if(isset($_SERVER['HTTP_REFERER'])){ $referer = strtolower($_SERVER['HTTP_REFERER']); $mobile_url = strtolower($_SERVER['HTTP_HOST'].'/m/'); if(strpos($referer, $mobile_url) !== false){ return true; } } return false; }
By checking the value of $_SERVER['HTTP_REFERER'], you can get the page link from which the user entered the current page, thereby determining whether the user is using a mobile device Or a PC device.
Summary
The above three methods can all be used to determine the user terminal device type. Which method to choose mainly depends on the actual situation of the application. In practical applications, we can apply these methods to different scenarios to provide users with a more friendly experience. If a developer wants to develop a web application that is suitable for both mobile devices and PCs, then using the above method, it can be easily implemented.
The above is the detailed content of How does PHP determine whether it is opened on a mobile phone or a PC?. For more information, please follow other related articles on the PHP Chinese website!