PHP判断手机是IOS仍是Android

本文介绍了PHP判断手机是IOS仍是Android的三个小实例,要判断用户的手机是安卓的仍是ios的,搜了一下相关的资料,最终得到的结果分享给你们。php

实例1:主要是要用到HTTP_USER_AGENT,它表示的意思是用来检查浏览页面的访问者在用什么操做系统(包括版本号)浏览器(包括版本号)和用户我的偏好的代码。
监测代码以下:android

?ios

1浏览器

2微信

3iphone

4函数

5spa

6操作系统

7.net

8

9

10

11

12

13

14

15

16

17

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;

}

经过调用Objective-C这个函数,就能获取到手机的类型。

实例2:只须要一个判断就好

?

1

2

3

4

5

6

7

8

9

<?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';

}

?>

实例3:这个实例可能有些偏题不过也分享给你们

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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;

}

最后“买3赠一”,再为你们分享一个与本主题关系不大的小实例:

php判断页面是不是微信打开

?

1

2

3

4

5

6

7

8

9

10

11

$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];

}

相关文章
相关标签/搜索