通过一段时间的自学,也积累了一点有用的PHP小片段,发上来做个记录,顺便也帮助大家,以后遇到有用的片段还会继续更新滴!
直接跳转
延迟3秒跳转
4、移动客户端判断函数
wordpress版
1 | function wp_is_mobile() { |
4 | if ( isset( $is_mobile ) ) |
7 | if ( empty ( $_SERVER [ 'HTTP_USER_AGENT' ]) ) { |
9 | } elseif ( strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Mobile' ) !== false // many mobile devices (all iPhone, iPad, etc.) |
10 | || strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Android' ) !== false |
11 | || strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Silk/' ) !== false |
12 | || strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Kindle' ) !== false |
13 | || strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'BlackBerry' ) !== false |
14 | || strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Opera Mini' ) !== false |
15 | || strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Opera Mobi' ) !== false ) { |
discuz版
2 | $useragent =isset( $_SERVER [ 'HTTP_USER_AGENT' ]) ? $_SERVER [ 'HTTP_USER_AGENT' ] : '' ; |
3 | $useragent_commentsblock =preg_match( '|\(.*?\)|' , $useragent , $matches )>0? $matches [0]: '' ; |
4 | function CheckSubstrs( $substrs , $text ){ |
5 | foreach ( $substrs as $substr ) |
6 | if (false!== strpos ( $text , $substr )){ |
11 | $mobile_os_list = array ( 'Google Wireless Transcoder' , 'Windows CE' , 'WindowsCE' , 'Symbian' , 'Android' , 'armv6l' , 'armv5' , 'Mobile' , 'CentOS' , 'mowser' , 'AvantGo' , 'Opera Mobi' , 'J2ME/MIDP' , 'Smartphone' , 'Go.Web' , 'Palm' , 'iPAQ' ); |
12 | $mobile_token_list = array ( 'Profile/MIDP' , 'Configuration/CLDC-' , '160×160' , '176×220' , '240×240' , '240×320' , '320×240' , 'UP.Browser' , 'UP.Link' , 'SymbianOS' , 'PalmOS' , 'PocketPC' , 'SonyEricsson' , 'Nokia' , 'BlackBerry' , 'Vodafone' , 'BenQ' , 'Novarra-Vision' , 'Iris' , 'NetFront' , 'HTC_' , 'Xda_' , 'SAMSUNG-SGH' , 'Wapaka' , 'DoCoMo' , 'iPhone' , 'iPod' ); |
14 | $found_mobile =CheckSubstrs( $mobile_os_list , $useragent_commentsblock ) || |
15 | CheckSubstrs( $mobile_token_list , $useragent ); |
移动客户端判断函数使用方法:
任选上面两个版本(wordpress、discuz)之一,保存为 ismobile.php ,然后修改需要判断移动客户端的页面为
2 | header( "Content-Type:text/html; charset=GBK" ); |
7 | echo "<p>wp:移动客户端</p>" ; |
9 | echo "<p>wp:非移动客户端</p>" ; |
14 | echo "<p>dz:移动客户端</p>" ; |
16 | echo "<p>dz:非移动客户端</p>" ; |
下面是我自己的应用实例,自己加工的代码高亮程序的电脑版和手机版的自动切换
电脑版index.php切换至手机版mobile.php:
4 | header( "Location: mobile.php?id=" . $_GET [ 'id' ]); |
手机版mobile.php切换至电脑版index.php:
4 | header( "Location: index.php?id=" . $_GET [ 'id' ]); |