获取浏览器类型版本及操作系统类型
发表日期:2022-08-06 15:31:18 | 来源: | | 浏览(1070) 分类:PHP杂项
挺好用的,之前网上找的用了几年了,修复了有些特殊情况数组下标越界报错,扩展了部分平台的识别。
class Agent
{
/**
* 获取客户端浏览器信息 添加win10 edge浏览器判断
* @param null
* @return string
*/
public static function getBroswer()
{
if (isset($_SERVER['HTTP_USER_AGENT']))
{
$sys = $_SERVER['HTTP_USER_AGENT']; //获取用户代理字符串
}else{
$sys = ''; //获取用户代理字符串
}
if (stripos($sys, "Firefox/") > 0) {
preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
$exp[0] = "Firefox";
$exp[1] = $b[1]; //获取火狐浏览器的版本号
} elseif (stripos($sys, "Maxthon") > 0) {
preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou);
$exp[0] = "傲游";
$exp[1] = $aoyou[1];
} elseif (stripos($sys, "MSIE") > 0) {
preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
$exp[0] = "IE";
$exp[1] = $ie[1]; //获取IE的版本号
} elseif (stripos($sys, "OPR") > 0) {
preg_match("/OPR\/([\d\.]+)/", $sys, $opera);
$exp[0] = "Opera";
$exp[1] = $opera[1];
} elseif (stripos($sys, "Edge") > 0) {
//win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
preg_match("/Edge\/([\d\.]+)/", $sys, $Edge);
$exp[0] = "Edge";
$exp[1] = $Edge[1];
} elseif (stripos($sys, "Chrome") > 0) {
preg_match("/Chrome\/([\d\.]+)/", $sys, $google);
$exp[0] = "Chrome";
$exp[1] = $google[1]; //获取google chrome的版本号
} elseif (stripos($sys, 'rv:') > 0 && stripos($sys, 'Gecko') > 0) {
preg_match("/rv:([\d\.]+)/", $sys, $IE);
$exp[0] = "IE";
$exp[1] = $IE[1];
} elseif (stripos($sys, 'Safari') > 0) {
preg_match("/safari\/([^\s]+)/i", $sys, $safari);
$exp[0] = "Safari";
$exp[1] = $safari[1];
} elseif (stripos($sys, 'ApiPOST') !== false) {
preg_match("/apipost\/([^\s]+)/i", $sys, $ApiPOST);
$exp[0] = "ApiPOST";
$exp[1] = "Runtime";
} elseif (stripos($sys, 'PostmanRuntime') !== false) {
preg_match("/PostmanRuntime\/([^\s]+)/i", $sys, $Postman);
$exp[0] = "Postman";
$exp[1] = $Postman[1];
} elseif (stripos($sys, 'curl') !== false) {
preg_match("/curl\/([^\s]+)/i", $sys, $curl);
$exp[0] = "curl";
$exp[1] = $curl[1];
} else {
$exp[0] = "未知";
$exp[1] = "";
}
return $exp[0] . '(' . $exp[1] . ')';
}
/**
* 获取客户端操作系统信息包括win10
* @param null
* @return string
*/
public static function getOs()
{
if (isset($_SERVER['HTTP_USER_AGENT']))
{
$agent = $_SERVER['HTTP_USER_AGENT']; //获取用户代理字符串
}else{
$agent = ''; //获取用户代理字符串
}
if (preg_match('/Windows Phone/i', $agent)) {
$os = 'Windows Phone';
// } else if (preg_match('/win/i', $agent) && strpos($agent, '95')) {
// $os = 'Windows 95';
// } else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90')) {
// $os = 'Windows ME';
// } else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent)) {
// $os = 'Windows 98';
} else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent)) {
$os = 'Windows Vista';
} else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent)) {
$os = 'Windows 7';
} else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent)) {
$os = 'Windows 8';
} else if (preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent)) {
$os = 'Windows 10';#添加win10判断
} else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent)) {
$os = 'Windows XP';
} else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent)) {
$os = 'Windows 2000';
} else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent)) {
$os = 'Windows NT';
} else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent)) {
$os = 'Windows 32';
} else if (preg_match('/linux/i', $agent)) {
$os = 'Linux';
} else if (preg_match('/unix/i', $agent)) {
$os = 'Unix';
} else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent)) {
$os = 'SunOS';
} else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent)) {
$os = 'IBM OS/2';
} else if (preg_match('/Mac/i', $agent)) {
$os = 'Mac';
} else if (preg_match('/PowerPC/i', $agent)) {
$os = 'PowerPC';
} else if (preg_match('/AIX/i', $agent)) {
$os = 'AIX';
} else if (preg_match('/HPUX/i', $agent)) {
$os = 'HPUX';
} else if (preg_match('/NetBSD/i', $agent)) {
$os = 'NetBSD';
} else if (preg_match('/BSD/i', $agent)) {
$os = 'BSD';
} else if (preg_match('/OSF1/i', $agent)) {
$os = 'OSF1';
} else if (preg_match('/IRIX/i', $agent)) {
$os = 'IRIX';
} else if (preg_match('/FreeBSD/i', $agent)) {
$os = 'FreeBSD';
} else if (preg_match('/teleport/i', $agent)) {
$os = 'teleport';
} else if (preg_match('/flashget/i', $agent)) {
$os = 'flashget';
} else if (preg_match('/webzip/i', $agent)) {
$os = 'webzip';
} else if (preg_match('/offline/i', $agent)) {
$os = 'offline';
} elseif (preg_match('/ucweb|MQQBrowser|J2ME|IUC|3GW100|LG-MMS|i60|Motorola|MAUI|m9|ME860|maui|C8500|gt|k-touch|X8|htc|GT-S5660|UNTRUSTED|SCH|tianyu|lenovo|SAMSUNG/i', $agent)) {
$os = 'mobile';
} else {
$os = '未知';
}
return $os;
}
}例子:

- PHP(0)
- PHP杂项(34)
- ip白黑名单验证(0)
- 自实现getallheaders函数 获取header信息(0)
- 随机颜色(0)
- 中文全角半角互转(0)
- 字符和unicode互转(0)
- 日期时间友好展示为今天/昨天/前天/分钟/小时/天前(0)
- 递归创建多级目录(0)
- 字符串截取追加...(0)
- 批量反/转义特殊字符,数据库安全过滤写入,支持字符串、多维数组、对象集合(0)
- 压缩HTML,去换行空格,注释(0)
- 获取文章html中的img标签图片src地址(0)
- html过滤标签空格提取纯文本text(0)
- 自实现http_build_url 函数(0)
- 获取浏览器类型版本及操作系统类型(0)
- curl请求及注意事项(0)
- 二维数组排序(0)
- 文件字节大小换算KB、MB、GB、TB(0)
- 数字金额转汉字大写(0)
- 验证身份证号是否有效(0)
- 批量替换文件名(0)
- 批量修改照片文件名日期排序(0)
- PHP中文编程(0)
- PHP命令行工具-在Linux下写shell脚本(0)
- usort-使用用户自定义的比较函数对数组中的值进行排序(0)
- PHP 使用 curl 提交 json 格式数据(0)
- tp+nginx(0)
- nginx配置(0)
- PHP测试mysql数据库MYSQLI和PDO的性能比较(0)
- PHP高级扩展介绍,高手必学(0)
- 数组转XML(0)
- Memcache和Redis的区别(0)
- PHP7.0新特性(0)
- PHP中的魔术方法总结 :__construct, __destruct等..(0)
- PHP配置文件php.ini详解(0)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
宁公网安备 64010402001209号