PHP通過User Agent判定用戶設備

判定class

deviceCheck();
    }
 
    public function deviceCheck(){
         
        //獲取User Agent
        $this->ua = $_SERVER['HTTP_USER_AGENT'];
 
        if(strpos($this->ua,'iPhone') !== false){
            //iPhone
            $this->device = 'iphone';
        }
        elseif(strpos($this->ua,'iPad') !== false){
            //iPad
            $this->device = 'ipad';
        }
        elseif((strpos($this->ua,'Android') !== false) && (strpos($this->ua, 'Mobile') !== false)){
            //Android
            $this->device = 'android_m';
        }
        elseif(strpos($this->ua,'Android') !== false){
            //Android
            $this->device = 'android_t';
        }
        else{
            $this->device = 'pc';
        }
    }
 
    public function getDevice(){
        return $this->device;
    }
}
?>

執行

getDevice();
?>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *