Mobile Device Detection Framework (PHP)

//initialize all known devices as false
$iPod = false;
$iPhone = false;
$iPad = false;
$iOS = false;
$webOSPhone = false;
$webOSTablet = false;
$webOS = false;
$BlackBerry9down = false;
$BlackBerry10 = false;
$RimTablet = false;
$BlackBerry = false;
$NokiaSymbian = false;
$Symbian = false;
$AndroidTablet = false;
$AndroidPhone = false;
$Android = false;
$WindowsPhone = false;
$WindowsTablet = false;
$Windows = false;
$Tablet = false;
$Phone = false;

//Detect special conditions devices & types (tablet/phone form factor)
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPod")){
    $iPod = true;
    $Phone = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPhone")){
    $iPhone = true;
    $Phone = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPad")){
    $iPad = true;
    $Tablet = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"webOS")){

    $webOS = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Pre") || stripos($_SERVER['HTTP_USER_AGENT'],"Pixi")){
        $webOSPhone = true;
        $Phone = true;
    }
    if(stripos($_SERVER['HTTP_USER_AGENT'],"TouchPad")){
        $webOSTablet = true;
        $Tablet = true;
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"BlackBerry")){
    $BlackBerry = true;
    $BlackBerry9down = true;
    $Phone = true;
{
if(stripos($_SERVER['HTTP_USER_AGENT'],"BB10")){
    $BlackBerry = true;
    $BlackBerry10 = true;
    $Phone = true;
{
if(stripos($_SERVER['HTTP_USER_AGENT'],"RIM Tablet")){
    $BlackBerry = true;
    $RimTablet = true;
    $Tablet = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"SymbianOS")){
    $Symbian = true;
    $NokiaSymbian = true;
    $Phone = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"Android")){

    $Android = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"mobile")){
        $AndroidPhone = true;
        $Phone = true;
    }else{
        $AndroidTablet = true;
        $Tablet = true;
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows")){

    $Windows = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Touch")){
        $WindowsTablet = true;
        $Tablet = true;
    }
    if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows Phone")){
        $WindowsPhone = true;
        $Phone = true;
    }
}



//Target form factors
if( $Phone ){
    //we're phone form factor
}else if( $Tablet ){
    //we're a tablet form factor
}else{
    //we're neither a phone or tablet
}




//Target operating systems
if( $iOS ){
    //we're an iOS device
}else if( $Android ){
    //we're an Android device
}else if( $Windows ){
    //we're a Windows device
}else if( $BlackBerry ){
    //we're a BlackBerry device
}else if( $webOS ){
    //we're a webOS device
}else if( $Symbian ){
    //we're a Symbian device
}else{
    //we're neither a phone or tablet
}




//Target individual devices
if( $iPod || $iPhone ){
    //we're an iPhone/iPod touch -- do something here
}else if($iPad){
    //we're an iPad -- do something here
}else if($AndroidPhone){
    //we're an Android Phone -- do something here
}else if($AndroidTablet){
    //we're an Android Tablet -- do something here
}else if($WindowsPhone){
    //we're an Windows Phone -- do something here
}else if($WindowsTablet){
    //we're an Windows Tablet -- do something here
}else if($webOSPhone){
    //we're a webOS phone -- do something here
}else if($webOSTablet){
    //we're a webOS tablet -- do something here
}else if($BlackBerry9down){
    //we're an outdated BlackBerry phone -- do something here
}else if($BB10){
    //we're an new BlackBerry phone -- do something here
}else if($RimTablet){
    //we're a RIM/BlackBerry Tablet -- do something here
}else if($NokiaSymbian){
    //we're a Nokia Symbian device -- do something here
}else{
    //we're not a known device.
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注