首页 > 编程语言 >PHP 检测访问设备是移动端还是PC端

PHP 检测访问设备是移动端还是PC端

时间:2023-02-10 12:14:57浏览次数:54  
标签:iPad 访问 Phone PC deviceType userAgent Android PHP

$userAgent = $_SERVER['HTTP_USER_AGENT'];
$devices = array(
  "iPhone" => "(iPhone)",
  "Android" => "(Android)",
  "Windows Phone" => "(Windows Phone)",
  "iPad" => "(iPad)"
);

$deviceType = "PC";

foreach ($devices as $device => $regexp) {
  if (preg_match("/" . $regexp . "/i", $userAgent)) {
    $deviceType = $device;
    break;
  }
}

if ($deviceType === "PC") {
  // PC端代码
} else {
  // 移动端代码
}

 

标签:iPad,访问,Phone,PC,deviceType,userAgent,Android,PHP
From: https://www.cnblogs.com/blts/p/17108436.html

相关文章