function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
你可以这样使用它:
// Use the function
if(isMobile()){
// Do something for only mobile users
}
else {
// Do something for only desktop users
}
要将用户重定向到您的移动站点,我会这样做:
// Create the function, so you can use it
function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, redirect them
if(isMobile()){
header("Location: http://m.yoursite.com/");
}
<?php
//include file
include_once 'user_agent.php';
//create an instance of UserAgent class
$ua = new UserAgent();
//if site is accessed from mobile, then redirect to the mobile site.
if($ua->is_mobile()){
header("Location:http://m.codexworld.com");
exit;
}
?>
//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 } ?>
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";
}
}