PHP简单判断iPhone、iPad、Android及PC设备的方法

2025-05-29 0 67

本文实例讲述了PHP简单判断iPhone、iPadAndroidPC设备的方法。分享给大家供大家参考,具体如下:

因为工作需要我们需要知道是什么样了用户访问了我网站了,现在的移动设备种类多了,下面我们一起来看小编整理的一段PHP判断iPhone、iPadAndroidPC设备的例子.

我将使用Windows系统的设备定为PC,毕竟博客面向中国用户,大部分家用设备还是用的Windows系统.

原理是判断浏览器提交的USER AGENT,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22
<?php

//获取USER AGENT

$agent = strtolower($_SERVER['HTTP_USER_AGENT']);

//分析数据

$is_pc = (strpos($agent, 'windows nt')) ? true : false;

$is_iphone = (strpos($agent, 'iphone')) ? true : false;

$is_ipad = (strpos($agent, 'ipad')) ? true : false;

$is_android = (strpos($agent, 'android')) ? true : false;

//输出数据

if($is_pc){

echo "这是PC";

}

if($is_iphone){

echo "这是iPhone";

}

if($is_ipad){

echo "这是iPad";

}

if($is_android){

echo "这是Android";

}

?>

如果你只判断是否为iphone设备可以如下来进行操作,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11
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;

}

希望本文所述对大家PHP程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP简单判断iPhone、iPad、Android及PC设备的方法 https://www.kuaiidc.com/96827.html

相关文章

发表评论
暂无评论