判断用户是否使用移动设备使用PHP浏览我的网站的最简单方法是什么?
我遇到过许多类,你可以使用,但我希望一个简单的if条件!
有什么办法可以让我这么做吗?
判断用户是否使用移动设备使用PHP浏览我的网站的最简单方法是什么?
我遇到过许多类,你可以使用,但我希望一个简单的if条件!
有什么办法可以让我这么做吗?
当前回答
我发现移动检测非常简单,你可以使用isMobile()函数:)
其他回答
没有可靠的方法。您也许可以查看user-agent字符串,但这可能会被欺骗或省略。或者,您也可以使用GeoIP服务来查找客户机的IP地址,但同样,这可以很容易地避免。
也许结合一些javascript和PHP可以达到这个目的
<?php
$string = '<script>';
$string .= 'if ( /Opera|OPR\/|Puffin|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { ';
$string .= ' alert("CELL")';
$string .= ' } else {';
$string .= ' alert("NON CELL")';
$string .= ' } ';
$string .= '</script>';
echo $string;
?>
我也将其与纯javascript一起使用
我发现移动检测非常简单,你可以使用isMobile()函数:)
<?php
//-- Very simple way
$useragent = $_SERVER['HTTP_USER_AGENT'];
$iPod = stripos($useragent, "iPod");
$iPad = stripos($useragent, "iPad");
$iPhone = stripos($useragent, "iPhone");
$Android = stripos($useragent, "Android");
$iOS = stripos($useragent, "iOS");
//-- You can add billion devices
$DEVICE = ($iPod||$iPad||$iPhone||$Android||$iOS);
if (!$DEVICE) { ?>
<!-- What you want for all non-mobile devices. Anything with all HTML, PHP, CSS, even full page codes-->
<?php }else{ ?>
<!-- What you want for all mobile devices. Anything with all HTML, PHP, CSS, even full page codes -->
<?php } ?>
简单地,你可以点击链接。它非常简单,很容易使用。我在用这个。它工作得很好。
http://mobiledetect.net/
像这样使用
//include the file
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
//do some code
}
// Any tablet device.
if( $detect->isTablet() ){
//do some code
}