Last active
December 22, 2015 19:29
-
-
Save kkosuge/6520346 to your computer and use it in GitHub Desktop.
PHP で UserAgent 判断するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UA { | |
public $ua = ''; | |
public function __construct() { | |
if (isset($_SERVER['HTTP_USER_AGENT'])) { | |
$this->ua = $_SERVER['HTTP_USER_AGENT']; | |
} | |
} | |
public function original() { | |
return $this->ua; | |
} | |
public function is_pc() { | |
if (!$this->is_mobile() && !$this->is_smartphone()) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function is_ios() { | |
if (preg_match('/iPhone|iPad/i', $this->ua)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function is_android() { | |
if (preg_match('/Android/i', $this->ua)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function is_mobile() { | |
$uas = array( | |
'DoCoMo', | |
'KDDI', | |
'DDIPOKET', | |
'UP.Browser', | |
'J-PHONE', | |
'Vodafone', | |
'SoftBank', | |
); | |
foreach ($uas as $val) { | |
$str = "/".$val."/i"; | |
if (preg_match($str, $this->ua)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
public function is_smartphone () { | |
$uas = array( | |
'Windows Phone', // Windows Phone | |
'iPhone', // Apple iPhone | |
'iPod', // Apple iPod touch | |
'dream', // Pre 1.5 Android | |
'CUPCAKE', // 1.5+ Android | |
'blackberry', // blackberry | |
'webOS', // Palm Pre Experimental | |
'Tizen', // Tizen | |
'incognito', // Other iPhone browser | |
'webmate' // Other iPhone browser | |
); | |
foreach ($uas as $val) { | |
$str = "/".$val."/i"; | |
if (preg_match($str, $this->ua)){ | |
return true; | |
} | |
} | |
// iPad is not smartphone | |
if (preg_match("/iPad/i", $this->ua)) { | |
return false; | |
} | |
// Android Mobile, Firefox OS and other mobile browsers | |
if ((!$this->is_mobile()) && preg_match("/Mobile/i", $this->ua)) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment