我的PHP之旅
判断访问是否来自手机或是电脑的PHP方法
post by:风子 2013-6-20 8:53
方法一
<?php  
header("Content-type:text/html;charset=utf-8");  
 function is_mobile(){  
    $user_agent = $_SERVER['HTTP_USER_AGENT'];  
    $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");  
    $is_mobile = false;  
    foreach ($mobile_agents as $device) {  
        if (stripos($user_agent, $device)!==false) {  
            $is_mobile = true;  
            break;  
        }  
    }  
    return $is_mobile;  
}  
if(is_mobile()){  
    echo "手机";  
}else{  
    echo "电脑";  
} 

方法二

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

$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i";

if(($ua == '' || preg_match($uachar, $ua)))
{
    echo '手机';
}else{
    echo '电脑';
}

判断来自于什么

function from_where()  
{  
    $useragent  = strtolower($_SERVER["HTTP_USER_AGENT"]);  
    // iphone  
    $is_iphone  = strripos($useragent,'iphone');  
    if($is_iphone){  
        return 'iphone';  
    }  
    // android  
    $is_android    = strripos($useragent,'android');  
    if($is_android){  
        return 'android';  
    }  
    // 微信  
    $is_weixin  = strripos($useragent,'micromessenger');  
    if($is_weixin){  
        return 'weixin';  
    }  
    // ipad  
    $is_ipad    = strripos($useragent,'ipad');  
    if($is_ipad){  
        return 'ipad';  
    }  
    // ipod  
    $is_ipod    = strripos($useragent,'ipod');  
    if($is_ipod){  
        return 'ipod';  
    }  
    // pc电脑  
    $is_pc = strripos($useragent,'windows nt');  
    if($is_pc){  
        return 'pc';  
    }  
    return 'other';  
}  

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容