本文目录导读:

在 PHP 中区分用户端(即识别用户的设备类型、操作系统、浏览器等),主要有以下几种方法:
使用 $_SERVER['HTTP_USER_AGENT'](最常用)
这是最基础的方法,通过解析 User-Agent 字符串来识别:
<?php
function detectDevice() {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
// 检测移动设备
$isMobile = false;
$mobileAgents = [
'Mobile', 'Android', 'iPhone', 'iPad', 'iPod',
'Windows Phone', 'BlackBerry', 'Opera Mini', 'IEMobile'
];
foreach ($mobileAgents as $agent) {
if (stripos($userAgent, $agent) !== false) {
$isMobile = true;
break;
}
}
// 检测操作系统
$os = 'Unknown';
if (stripos($userAgent, 'Windows') !== false) $os = 'Windows';
elseif (stripos($userAgent, 'Mac OS') !== false) $os = 'Mac OS';
elseif (stripos($userAgent, 'Linux') !== false) $os = 'Linux';
elseif (stripos($userAgent, 'Android') !== false) $os = 'Android';
elseif (stripos($userAgent, 'iPhone') !== false || stripos($userAgent, 'iPad') !== false) $os = 'iOS';
// 检测浏览器
$browser = 'Unknown';
if (stripos($userAgent, 'Chrome') !== false) $browser = 'Chrome';
elseif (stripos($userAgent, 'Firefox') !== false) $browser = 'Firefox';
elseif (stripos($userAgent, 'Safari') !== false) $browser = 'Safari';
elseif (stripos($userAgent, 'Edge') !== false) $browser = 'Edge';
elseif (stripos($userAgent, 'IE') !== false) $browser = 'Internet Explorer';
return [
'is_mobile' => $isMobile,
'os' => $os,
'browser' => $browser,
'user_agent' => $userAgent
];
}
// 使用
$device = detectDevice();
echo "操作系统: " . $device['os'];
echo "浏览器: " . $device['browser'];
echo $device['is_mobile'] ? "移动端" : "PC端";
使用第三方库(推荐)
使用 Mobile_Detect(轻量级)
// 首先通过 Composer 安装
// composer require mobiledetect/mobiledetectlib
require_once 'vendor/autoload.php';
use Detection\MobileDetect;
$detect = new MobileDetect();
// 检测设备类型
if ($detect->isMobile()) {
echo "这是移动设备";
}
if ($detect->isTablet()) {
echo "这是平板";
}
if ($detect->isPhone()) {
echo "这是手机";
}
// 检测操作系统
if ($detect->isIOS()) {
echo "iOS 设备";
}
if ($detect->isAndroidOS()) {
echo "Android 设备";
}
// 获取具体设备
$deviceType = $detect->isMobile() ? 'Mobile' : 'Desktop';
if ($detect->isTablet()) {
$deviceType = 'Tablet';
}
// 检测特定平台
if ($detect->isiPhone()) {
echo "这是 iPhone";
}
if ($detect->isSamsung()) {
echo "这是三星设备";
}
使用 jaybizzle/crawler-detect(检测爬虫)
// composer require jaybizzle/crawler-detect
use Jaybizzle\CrawlerDetect\CrawlerDetect;
$crawlerDetect = new CrawlerDetect;
// 检查是否为爬虫
if ($crawlerDetect->isCrawler()) {
echo "这是搜索引擎爬虫";
}
高级检测方案
结合 IP 和 User-Agent 进行更精确的判断
class DeviceDetector {
private $userAgent;
private $ip;
private $acceptLanguage;
public function __construct() {
$this->userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$this->ip = $_SERVER['REMOTE_ADDR'] ?? '';
$this->acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
}
public function getDeviceInfo() {
$info = [
'device_type' => $this->detectDeviceType(),
'is_bot' => $this->detectBot(),
'language' => $this->detectLanguage()
];
return $info;
}
private function detectDeviceType() {
// 综合判断
$isMobile = preg_match('/Mobile|Android|iPhone|iPad/i', $this->userAgent);
$isTablet = preg_match('/iPad|Tablet/i', $this->userAgent);
if ($isTablet) return 'tablet';
if ($isMobile) return 'mobile';
return 'desktop';
}
private function detectBot() {
$botKeywords = ['bot', 'crawler', 'spider', 'curl', 'wget'];
foreach ($botKeywords as $keyword) {
if (stripos($this->userAgent, $keyword) !== false) {
return true;
}
}
return false;
}
private function detectLanguage() {
$lang = substr($this->acceptLanguage, 0, 5);
return $lang ?: 'unknown';
}
}
// 使用
$detector = new DeviceDetector();
$deviceInfo = $detector->getDeviceInfo();
print_r($deviceInfo);
使用设备识别服务(云端 API)
如果需求比较复杂,可以使用第三方云服务:
// 示例:使用 51Degrees 或 DeviceAtlas 等云服务
class CloudDeviceDetector {
private $apiKey;
public function __construct($apiKey) {
$this->apiKey = $apiKey;
}
public function detect($userAgent) {
// 调用云 API
$url = "https://api.deviceatlas.com/v2/complete?user-agent=" . urlencode($userAgent);
$ch = curl_init($url);
// ... 处理 API 响应
}
}
完整的企业级解决方案
<?php
// 简单但完整的设备检测类
class UserDeviceDetector {
private $userAgent;
public function __construct() {
$this->userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
}
public function analyze() {
return [
'browser' => $this->getBrowser(),
'platform' => $this->getPlatform(),
'device' => $this->getDevice(),
'is_mobile' => $this->isMobile(),
'is_tablet' => $this->isTablet(),
'is_desktop' => $this->isDesktop()
];
}
private function getBrowser() {
if (preg_match('/MSIE/i', $this->userAgent)) return 'Internet Explorer';
if (preg_match('/Edge/i', $this->userAgent)) return 'Edge';
if (preg_match('/Chrome/i', $this->userAgent)) return 'Chrome';
if (preg_match('/Firefox/i', $this->userAgent)) return 'Firefox';
if (preg_match('/Safari/i', $this->userAgent)) return 'Safari';
return 'Unknown';
}
private function getPlatform() {
if (preg_match('/Windows NT 10/i', $this->userAgent)) return 'Windows 10';
if (preg_match('/Windows NT 6.1/i', $this->userAgent)) return 'Windows 7';
if (preg_match('/Mac OS X/i', $this->userAgent)) return 'macOS';
if (preg_match('/Android/i', $this->userAgent)) return 'Android';
if (preg_match('/iPhone|iPad|iPod/i', $this->userAgent)) return 'iOS';
if (preg_match('/Linux/i', $this->userAgent)) return 'Linux';
return 'Unknown';
}
private function isMobile() {
return preg_match('/Mobile|Android|iPhone|Windows Phone/i', $this->userAgent);
}
private function isTablet() {
return preg_match('/iPad|Tablet/i', $this->userAgent);
}
private function isDesktop() {
return !$this->isMobile() && !$this->isTablet();
}
}
// 使用示例
$detector = new UserDeviceDetector();
$deviceInfo = $detector->analyze();
?>
<!-- 在模板中使用 -->
<?php if ($deviceInfo['is_mobile']): ?>
<div class="mobile-only">移动端专属内容</div>
<?php endif; ?>
<?php if ($deviceInfo['device'] == 'desktop'): ?>
<div class="desktop-only">桌面端专属内容</div>
<?php endif; ?>
Demo 演示效果
// 完整的使用示例
$userAgent = $_SERVER['HTTP_USER_AGENT'];
echo "你的 User Agent: " . $userAgent . "<br>";
// 检测设备类型
if (preg_match('/iPhone/', $userAgent)) {
echo "设备: iPhone<br>";
} elseif (preg_match('/iPad/', $userAgent)) {
echo "设备: iPad<br>";
} elseif (preg_match('/Android/', $userAgent)) {
echo "设备: Android<br>";
} else {
echo "设备: PC/其他<br>";
}
// 检测结果输出
echo "浏览器: " . $detector->analyze()['browser'] . "<br>";
echo "平台: " . $detector->analyze()['platform'] . "<br>";
最佳实践建议
- 不要完全依赖 User-Agent,部分用户可能修改它
- 缓存检测结果,避免重复解析
- 考虑隐私问题,中国地区可能需要遵守相关法规
- 使用库而非手写,第三方库更完善
- 移动端优先设计,确保响应式布局
推荐优先使用 Mobile_Detect 库,它经过充分测试且维护良好,处理了很多边界情况。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播