判断用户是否使用移动设备使用PHP浏览我的网站的最简单方法是什么?
我遇到过许多类,你可以使用,但我希望一个简单的if条件!
有什么办法可以让我这么做吗?
判断用户是否使用移动设备使用PHP浏览我的网站的最简单方法是什么?
我遇到过许多类,你可以使用,但我希望一个简单的if条件!
有什么办法可以让我这么做吗?
当前回答
function isMobileDev(){
if(!empty($_SERVER['HTTP_USER_AGENT'])){
$user_ag = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/(Mobile|Android|Tablet|GoBrowser|[0-9]x[0-9]*|uZardWeb\/|Mini|Doris\/|Skyfire\/|iPhone|Fennec\/|Maemo|Iris\/|CLDC\-|Mobi\/)/uis',$user_ag)){
return true;
};
};
return false;
}
其他回答
function isMobileDev(){
if(!empty($_SERVER['HTTP_USER_AGENT'])){
$user_ag = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/(Mobile|Android|Tablet|GoBrowser|[0-9]x[0-9]*|uZardWeb\/|Mini|Doris\/|Skyfire\/|iPhone|Fennec\/|Maemo|Iris\/|CLDC\-|Mobi\/)/uis',$user_ag)){
return true;
};
};
return false;
}
如果您的服务器支持get_browser(自PHP 4起可用),则非常简单。它们有一个内置的功能来满足你的要求。
参考:https://www.php.net/manual/en/function.get-browser.php
<?php
$browser = get_browser(null, true);
if($browser['ismobiledevice']) {
// Device is mobile
}
?>
一个更简单的解决方案似乎在大多数情况下都有效:
function funcDeviceType() {
// INFO ABOUT THE BROWSER/DEVICE USER IS VISITING ON
$useragent = $_SERVER["HTTP_USER_AGENT"];
if(stripos($useragent, "mobile")!== false){
return "mobile device";
}else{
return "desktop or laptop computer";
}
}
简单地,你可以点击链接。它非常简单,很容易使用。我在用这个。它工作得很好。
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
}
<?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 } ?>