检测移动设备上的访问[重复]
Posted
技术标签:
【中文标题】检测移动设备上的访问[重复]【英文标题】:Detection of accessing on a mobile device [duplicate] 【发布时间】:2015-02-25 08:37:20 【问题描述】:我有一个正在开发的网站,我计划在它旁边发布一个配套应用程序,因为它在移动设备上看起来不像在桌面上那么好。还有很多性能问题。我注意到如果您使用移动设备,某些网站会通知您转到不同的页面,我想做类似的事情,除了弹出一条消息。我确定我可以处理消息部分,但我需要帮助的是检测移动设备使用情况的方法。我通过简单的谷歌搜索看到了这个网站:
http://detectmobilebrowsers.com/
但我完全不知道如何用 JS 来实现它。
提前感谢您的帮助!
【问题讨论】:
稍微搜索一下怎么样? Detecting a mobile browser detectmobilebrowsers.com 上有一个 javascript 按钮,其中包含他们正在使用的脚本。它使用与给定浏览器的用户代理字符串匹配的正则表达式模式。 【参考方案1】:试试这个
/**
* Mobile Detect
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class Mobile_Detect
protected $accept;
protected $userAgent;
protected $isMobile = false;
protected $isandroid = null;
protected $isAndroidtablet = null;
protected $isIphone = null;
protected $isIpad = null;
protected $isBlackberry = null;
protected $isBlackberrytablet = null;
protected $isOpera = null;
protected $isPalm = null;
protected $isWindows = null;
protected $isWindowsphone = null;
protected $isGeneric = null;
protected $devices = array(
"android" => "android.*mobile",
"androidtablet" => "android(?!.*mobile)",
"blackberry" => "blackberry",
"blackberrytablet" => "rim tablet os",
"iphone" => "(iphone|ipod)",
"ipad" => "(ipad)",
"palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
"windows" => "windows ce; (iemobile|ppc|smartphone)",
"windowsphone" => "windows phone os",
"generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)");
public function __construct()
$this->userAgent = $_SERVER['HTTP_USER_AGENT'];
$this->accept = $_SERVER['HTTP_ACCEPT'];
if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))
$this->isMobile = true;
elseif (strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0)
$this->isMobile = true;
else
foreach ($this->devices as $device => $regexp)
if ($this->isDevice($device))
$this->isMobile = true;
/**
* Overloads isAndroid() | isAndroidtablet() | isIphone() | isIpad() | isBlackberry() | isBlackberrytablet() | isPalm() | isWindowsphone() | isWindows() | isGeneric() through isDevice()
*
* @param string $name
* @param array $arguments
* @return bool
*/
public function __call($name, $arguments)
$device = substr($name, 2);
if ($name == "is" . ucfirst($device) && array_key_exists(strtolower($device), $this->devices))
return $this->isDevice($device);
else
trigger_error("Method $name not defined", E_USER_WARNING);
/**
* Returns true if any type of mobile device detected, including special ones
* @return bool
*/
public function isMobile()
return $this->isMobile;
protected function isDevice($device)
$var = "is" . ucfirst($device);
$return = $this->$var === null ? (bool) preg_match("/" . $this->devices[strtolower($device)] . "/i", $this->userAgent) : $this->$var;
if ($device != 'generic' && $return == true)
$this->isGeneric = false;
return $return;
//call this way
$detect= new Mobile_Detect();
if ($detect->isMobile())
$_SESSION['mobile']="mobile";
【讨论】:
以上是关于检测移动设备上的访问[重复]的主要内容,如果未能解决你的问题,请参考以下文章