PHP判断手机浏览器实现网页自适应
作者:秋了秋 发表时间:2015年04月30日
做网页的自适应有很多种方法,可以通过css,具体可以看这篇文章《自适应网页制作教程》,用css做自适应网页可以说是最简单快捷的了,还可以通过js判断,原理和css差不多都是判断用户的屏幕分辨率大小从而改变css样式。除了这两种方法之外,还可以通过php来判断客户端的种类从而执行不同的操作。在php页面中加入以下判断程序代码:
/*判断是否移动端访问*/ function is_mobile() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_browser = Array( "mqqbrowser", //手机QQ浏览器 "opera mobi", //手机Opera "juc","iuc",//UC浏览器 "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod", "iemobile", "windows ce",//windows phone "240x320","480x640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte" );$is_mobile = false; foreach ($mobile_browser as $device) { if (stristr($user_agent, $device)) { $is_mobile = true;break;}} return $is_mobile;}
然后在需要不同设备下加入不同的代码处添加以下代码:
<?php if (is_mobile() ): ?> // 移动端输出内容(如引入新的样式):<link rel="stylesheet" href="**/style.css" type="text/css" media="screen" /> <?php else: ?> // PC端输出内容(一般为正常网页内容) <?php endif; ?>
4
文章作者: “秋了秋”个人博客,本站鼓励原创。
转载请注明本文地址:http://netblog.cn/blog/341.html