日志记录辅助函数测试

This commit is contained in:
2024-12-03 16:35:01 +08:00
parent 5cfb9d44f6
commit ac91b49ba4

View File

@@ -26,4 +26,68 @@ if (!function_exists('ase_decode')) {
{
return base64_decode(substr(base64_decode($data), 30));
}
}
// 获取用户设备类型
if (!function_exists('get_device_type')) {
function get_device_type(): array
{
// 获取HTTP请求头中的User-Agent字段
$userAgent = $_SERVER['HTTP_USER_AGENT'];
// 初始化设备信息数组
$deviceInfo = [
'isMobile' => false,
'isTablet' => false,
'deviceType' => 'Desktop',
'browser' => '',
];
// 检查是否为移动设备
if (preg_match('/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $userAgent)) {
$deviceInfo['isMobile'] = true;
$deviceInfo['deviceType'] = 'Mobile';
}
// 检查是否为平板设备
if (preg_match('/tablet|ipad|playbook|silk/i', $userAgent)) {
$deviceInfo['isTablet'] = true;
$deviceInfo['deviceType'] = 'Tablet';
}
// 确定浏览器类型
if (strpos($userAgent, 'Firefox') !== false) {
$deviceInfo['browser'] = 'Mozilla Firefox';
} elseif (strpos($userAgent, 'Opera Mini') !== false || strpos($userAgent, 'Opera') !== false) {
$deviceInfo['browser'] = 'Opera';
} elseif (strpos($userAgent, 'Chrome') !== false) {
$deviceInfo['browser'] = 'Google Chrome';
} elseif (strpos($userAgent, 'Safari') !== false && strpos($userAgent, 'Version/') !== false) {
$deviceInfo['browser'] = 'Apple Safari';
} elseif (strpos($userAgent, 'MSIE') !== false || strpos($userAgent, 'Trident/7') !== false) {
$deviceInfo['browser'] = 'Internet Explorer';
}
// 确定操作系统平台
if (strpos($userAgent, 'Windows NT 10.0') !== false) {
$deviceInfo['platform'] = 'Windows 10';
} elseif (strpos($userAgent, 'Windows NT 6.3') !== false) {
$deviceInfo['platform'] = 'Windows 8.1';
} elseif (strpos($userAgent, 'Windows NT 6.2') !== false) {
$deviceInfo['platform'] = 'Windows 8';
} elseif (strpos($userAgent, 'Windows NT 6.1') !== false) {
$deviceInfo['platform'] = 'Windows 7';
} elseif (strpos($userAgent, 'Macintosh; Intel Mac OS X') !== false) {
$deviceInfo['platform'] = 'Mac OS X';
} elseif (strpos($userAgent, 'iPad') !== false) {
$deviceInfo['platform'] = 'iOS';
} elseif (strpos($userAgent, 'iPhone') !== false) {
$deviceInfo['platform'] = 'iOS';
} elseif (strpos($userAgent, 'Android') !== false) {
$deviceInfo['platform'] = 'Android';
} else {
$deviceInfo['platform'] = '未知';
}
return $deviceInfo;
}
}