Home >Backend Development >PHP Problem >How to determine if PHP is developing for Android or iOS
php method to determine whether it is Android or ios: 1. Create a PHP sample file; 2. Implement the judgment through the "function get_device_type(){...}" method; 3. Call the "Objective-C" function Just get the phone type.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
PHP determines whether the mobile phone is IOS or Android
This article introduces three small examples of using PHP to determine whether a mobile phone is IOS or Android. To determine whether the user's mobile phone is Android or iOS, I searched for relevant information and shared the final results with everyone. .
Example 1: Mainly uses HTTP_USER_AGENT, which means it is used to check what operating system (including version number) browser (including version number) the visitor browsing the page is using. number) and the user's personal preference code.
The monitoring code is as follows:
function get_device_type() { //全部变成小写字母 $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $type = 'other'; //分别进行判断 if(strpos($agent, 'iphone') || strpos($agent, 'ipad')) { $type = 'ios'; } if(strpos($agent, 'android')) { $type = 'android'; } return $type; }
By calling the Objective-C function, the type of mobile phone can be obtained.
Example 2: All you need is one judgment
<?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){ echo 'systerm is IOS'; }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){ echo 'systerm is Android'; }else{ echo 'systerm is other'; } ?>
Example 3: This example may be a little off topic but I will share it with you.
function get_device_type() { //全部变成小写字母 $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $type ='other'; //分别进行判断 if(strpos($agent,'iphone') || strpos($agent,'ipad')) { $type ='ios'; } if(strpos($agent,'android')) { $type ='android'; } return$type; }
Finally, "Buy 3 and get one free", let me share with you a small example that has little to do with this topic:
php determines whether the page is opened by WeChat
$user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, 'MicroMessenger') === false) { // 非微信浏览器禁止浏览 echo "HTTP/1.1 401 Unauthorized"; } else { // 微信浏览器,允许访问 echo "MicroMessenger"; // 获取版本号 preg_match('/.*?(MicroMessenger\/([0-9.]+))\s*/', $user_agent, $matches); echo '<br>Version:'.$matches[2]; }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine if PHP is developing for Android or iOS. For more information, please follow other related articles on the PHP Chinese website!