Quantcast
Channel: プラグイン検証用-FTPup
Viewing all articles
Browse latest Browse all 88

UA判定

$
0
0
/* ----------------------------------------------------------------------------------------- */
// UA判定
// https://w3g.jp/blog/php_ua_sniffing2015
class UserAgent{
 private $ua;
 private $device;
 public function set(){
 $this->ua = mb_strtolower($_SERVER['HTTP_USER_AGENT']);
 if(strpos($this->ua,'iphone') !== false){
 $this->device = 'mobile';
 }elseif(strpos($this->ua,'ipod') !== false){
 $this->device = 'mobile';
 }elseif((strpos($this->ua,'android') !== false) && (strpos($this->ua, 'mobile') !== false)){
 $this->device = 'mobile';
 }elseif((strpos($this->ua,'windows') !== false) && (strpos($this->ua, 'phone') !== false)){
 $this->device = 'mobile';
 }elseif((strpos($this->ua,'firefox') !== false) && (strpos($this->ua, 'mobile') !== false)){
 $this->device = 'mobile';
 }elseif(strpos($this->ua,'blackberry') !== false){
 $this->device = 'mobile';
 }elseif(strpos($this->ua,'ipad') !== false){
 $this->device = 'tablet';
 }elseif((strpos($this->ua,'windows') !== false) && (strpos($this->ua, 'touch') !== false && (strpos($this->ua, 'tablet pc') == false))){
 $this->device = 'tablet';
 }elseif((strpos($this->ua,'android') !== false) && (strpos($this->ua, 'mobile') === false)){
 $this->device = 'tablet';
 }elseif((strpos($this->ua,'firefox') !== false) && (strpos($this->ua, 'tablet') !== false)){
 $this->device = 'tablet';
 }elseif((strpos($this->ua,'kindle') !== false) || (strpos($this->ua, 'silk') !== false)){
 $this->device = 'tablet';
 }elseif((strpos($this->ua,'playbook') !== false)){
 $this->device = 'tablet';
 }else{
 $this->device = 'others';
 }
 return $this->device;
 }
}

使い方

 require_once "ua.class.php";
 $ua = new UserAgent();
 if($ua->set() === "mobile") {
 //この中のコードはスマホにのみ適用
 }elseif($ua->set() === "tablet"){
 //この中のコードはタブレットにのみ適用
 }else{
 //この中のコードはスマホとタブレット以外に適用
 }

 


Viewing all articles
Browse latest Browse all 88

Trending Articles