判断用户是否使用移动设备使用PHP浏览我的网站的最简单方法是什么?

我遇到过许多类,你可以使用,但我希望一个简单的if条件!

有什么办法可以让我这么做吗?


当前回答

如果您的服务器支持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 
}
?>

其他回答

<?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 } ?>

也许结合一些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一起使用

你也可以使用第三方api通过用户代理字符串进行设备检测。其中一个这样的服务是www.useragentinfo.co。只需注册并获得你的api令牌,以下是你如何通过PHP获得设备信息:

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
// get api token at https://www.useragentinfo.co/
$token = "<api-token>";
$url = "https://www.useragentinfo.co/api/v1/device/";

$data = array('useragent' => $useragent);

$headers = array();
$headers[] = "Content-type: application/json";
$headers[] = "Authorization: Token " . $token;

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status != 200 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

curl_close($curl);

echo $json_response;
?>

如果访问者使用的是iPhone,下面是示例回复:

{
  "device_type":"SmartPhone",
  "browser_version":"5.1",
  "os":"iOS",
  "os_version":"5.1",
  "device_brand":"Apple",
  "bot":false,
  "browser":"Mobile Safari",
  "device_model":"iPhone"
}

如果您的服务器支持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 
}
?>

您只需要包含user_agent.php文件,该文件可以在PHP页面中的移动设备检测中找到,并使用以下代码。

<?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;
}
?>