$_SERVER['HTTP_USER_AGENT'];
ua即请求头里的user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36
可以用来判断浏览器类型,微信里可以获取微信版本
public function getOS($id)
{
$ua = $_SERVER['HTTP_USER_AGENT'];//这里只进行IOS和Android两个操作系统的判断,其他操作系统原理一样
if (strpos($ua, 'Android') !== false) {//strpos()定位出第一次出现字符串的位置,这里定位为0
preg_match("/(?<=Android )[\d\.]{1,}/", $ua, $version);
$platform['os_platform'] = 'Android';
$platform['os_version'] = $version[0];
//echo 'Platform:Android OS_Version:'.$version[0];
} elseif (strpos($ua, 'iPhone') !== false) {
preg_match("/(?<=CPU iPhone OS )[\d\_]{1,}/", $ua, $version);
$platform['os_platform'] = 'iPhone';
$platform['os_version'] = str_replace('_', '.', $version[0]);
echo 'Platform:iPhone OS_Version:'.str_replace('_', '.', $version[0]);
} elseif (strpos($ua, 'iPad') !== false) {
preg_match("/(?<=CPU OS )[\d\_]{1,}/", $ua, $version);
$platform['os_platform'] = 'iPad';
$platform['os_version'] = str_replace('_', '.', $version[0]);
//echo 'Platform:iPad OS_Version:'.str_replace('_', '.', $version[0]);
} else{
$platform['os_platform'] = 'unknown';
$platform['os_version'] = 'unknown';
}
$platform['user_id'] = $id;
$is_platform_record = RecordPlatformModel::query()->where('user_id',$platform['user_id'])->first();
if ($is_platform_record) {
$platform_record = RecordPlatformModel::query()->where('user_id',$platform['user_id'])->update($platform);
} else {
$platform['created_at'] = date('Y-m-d H:i:s');
$platform_record = RecordPlatformModel::query()->insert($platform);
}
}