1. 程式人生 > >PHP CodeBase: 判斷使用者是否手機訪問

PHP CodeBase: 判斷使用者是否手機訪問

這裡就介紹一下,如何判斷使用者是否用手機訪問。
$agent = check_wap();
if( $agent )
{
header('Location: http://www.a.com);
exit;
}

// check if wap
function check_wap(){
// 先檢查是否為wap代理,準確度高
if(stristr( S E R

V E R [ H T T P V
I A ] , " w a p
" ) ) r e t u r n t r u e ; / / W M L . e l s e i f ( s t r p o s ( s t r t o u p p e r ( _SERVER['HTTP_VIA'],"wap")){ return true; } // 檢查瀏覽器是否接受 WML. elseif(strpos(strtoupper( SERVER[‘HTTP_ACCEPT’]),“VND.WAP.WML”) > 0){
return true;
}
//檢查USER_AGENT
elseif(preg_match(’/(blackberry|configuration/cldc|hp |hp-|htc |htc
|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i’, $_SERVER[‘HTTP_USER_AGENT’])){
return true;
}
else{
return false;
}
}
再來一個從PHP框架剝離的判斷是否為手機移動終端的函式:
function is_mobile_request()
{
S E R V E R [ A L L H T T P ] = i s s e t ( _SERVER['ALL_HTTP'] = isset( _SERVER[‘ALL_HTTP’]) ? $_SERVER[‘ALL_HTTP’] : ‘’;
m o b i l e b r o w s e r = 0 ; i f ( p r e g m a t c h ( / ( u p . b r o w s e r u p . l i n k m m p s y m b i a n s m a r t p h o n e m i d p w a p p h o n e i p h o n e i p a d i p o d a n d r o i d x o o m ) / i , s t r t o l o w e r ( mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower( _SERVER[‘HTTP_USER_AGENT’])))
m o b i l e b r o w s e r + + ; i f ( ( i s s e t ( mobile_browser++; if((isset( _SERVER[‘HTTP_ACCEPT’])) and (strpos(strtolower($_SERVER[‘HTTP_ACCEPT’]),‘application/vnd.wap.xhtml+xml’) !== false))
m o b i l e b r o w s e r + + ; i f ( i s s e t ( mobile_browser++; if(isset( _SERVER[‘HTTP_X_WAP_PROFILE’]))
m o b i l e b r o w s e r + + ; i f ( i s s e t ( mobile_browser++; if(isset( _SERVER[‘HTTP_PROFILE’]))
$mobile_browser++;
m o b i l e u a = s t r t o l o w e r ( s u b s t r ( mobile_ua = strtolower(substr( _SERVER[‘HTTP_USER_AGENT’],0,4));
m o b i l e a g e n t s = a r r a y ( w 3 c , a c s , a l a v , a l c a , a m o i , a u d i , a v a n , b e n q , b i r d , b l a c , b l a z , b r e w , c e l l , c l d c , c m d , d a n g , d o c o , e r i c , h i p t , i n n o , i p a q , j a v a , j i g s , k d d i , k e j i , l e n o , l g c , l g d , l g g , l g e , m a u i , m a x o , m i d p , m i t s , m m e f , m o b i , m o t , m o t o , m w b p , n e c , n e w t , n o k i , o p e r , p a l m , p a n a , p a n t , p h i l , p l a y , p o r t , p r o x , q w a p , s a g e , s a m s , s a n