Home >Backend Development >PHP Problem >How to determine if PHP is developing for Android or iOS

How to determine if PHP is developing for Android or iOS

藏色散人
藏色散人Original
2021-11-19 09:23:083330browse

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.

How to determine if PHP is developing for Android or iOS

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[&#39;HTTP_USER_AGENT&#39;], &#39;iPhone&#39;)||strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;iPad&#39;)){
 echo &#39;systerm is IOS&#39;;
}else if(strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;Android&#39;)){
 echo &#39;systerm is Android&#39;;
}else{
 echo &#39;systerm is other&#39;;
}
?>

Example 3: This example may be a little off topic but I will share it with you.

function get_device_type()
{
 //全部变成小写字母
 $agent = strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]);
 $type =&#39;other&#39;;
 //分别进行判断
 if(strpos($agent,&#39;iphone&#39;) || strpos($agent,&#39;ipad&#39;))
{
 $type =&#39;ios&#39;;
 }
 
 if(strpos($agent,&#39;android&#39;))
{
 $type =&#39;android&#39;;
 }
 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[&#39;HTTP_USER_AGENT&#39;]; 
if (strpos($user_agent, &#39;MicroMessenger&#39;) === false) { 
 // 非微信浏览器禁止浏览 
 echo "HTTP/1.1 401 Unauthorized"; 
} else { 
 // 微信浏览器,允许访问 
 echo "MicroMessenger"; 
 // 获取版本号 
 preg_match(&#39;/.*?(MicroMessenger\/([0-9.]+))\s*/&#39;, $user_agent, $matches); 
 echo &#39;<br>Version:&#39;.$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!

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
Previous article:Can’t PHP use for loop?Next article:Can’t PHP use for loop?