php 来判断手机品牌跟型号
时间: 2023-07-26 20:26:07 浏览: 57
你可以通过解析 HTTP 请求头中的 User-Agent 字段,来获取访问设备的品牌跟型号信息。以下是一个使用 PHP 解析 User-Agent 字段的示例代码:
```php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// 匹配 iPhone
if (preg_match('/iPhone/', $user_agent)) {
// 获取设备型号信息
if (preg_match('/iPhone[0-9],[0-9]/', $user_agent, $matches)) {
$device_model = $matches[0];
echo "This is an iPhone with model $device_model.";
} else {
echo "This is an iPhone.";
}
}
// 匹配 Samsung Galaxy
else if (preg_match('/Samsung/', $user_agent)) {
// 获取设备型号信息
if (preg_match('/SM-[A-Za-z0-9]+/', $user_agent, $matches)) {
$device_model = $matches[0];
echo "This is a Samsung Galaxy with model $device_model.";
} else {
echo "This is a Samsung Galaxy.";
}
}
// 其他设备
else {
echo "This is an unknown device.";
}
```
上述代码中,我们使用了正则表达式来匹配 iPhone 和 Samsung Galaxy 设备,并且通过不同的正则表达式来提取设备型号信息。你可以根据需要,添加更多品牌和型号的匹配规则。但需要注意的是,用户可能会使用伪造的 User-Agent 字段来欺骗服务器,所以这种方式并不是非常可靠。