Home > Article > Backend Development > PHP Get Browser Name Version Example Program_PHP Tutorial
To get the browser version, just use HTTP_USER_AGENT directly in php, but to specifically determine which browser and version we need to determine one by one.
Example 1
The code is as follows | Copy code | ||||
if (empty($_SERVER['HTTP_USER_AGENT'])) { return ''; } $agent = $_SERVER['HTTP_USER_AGENT']; $browser = ''; $browser_ver = ''; if (preg_match('/MSIEs([^s|;]+)/i', $agent, $regs)) { $browser = 'Internet Explorer'; $browser_ver = $regs[1]; } elseif (preg_match('/FireFox/([^s]+)/i', $agent, $regs)) { $browser = 'FireFox'; $browser_ver = $regs[1]; } elseif (preg_match('/Maxthon/i', $agent, $regs)) { $browser = '(Internet Explorer ' .$browser_ver. ') Maxthon'; $browser_ver = ''; } elseif (preg_match('/Opera[s|/]([^s]+)/i', $agent, $regs)) { $browser = 'Opera'; $browser_ver = $regs[1]; } elseif (preg_match('/OmniWeb/(v*)([^s|;]+)/i', $agent, $regs)) { $browser = 'OmniWeb'; $browser_ver = $regs[2]; } elseif (preg_match('/Netscape([d]*)/([^s]+)/i', $agent, $regs)) { $browser = 'Netscape'; $browser_ver = $regs[2]; } elseif (preg_match('/safari/([^s]+)/i', $agent, $regs)) { $browser = 'Safari'; $browser_ver = $regs[1]; } elseif (preg_match('/NetCaptors([^s|;]+)/i', $agent, $regs)) { $browser = '(Internet Explorer ' .$browser_ver. ') NetCaptor'; $browser_ver = $regs[1]; } elseif (preg_match('/Lynx/([^s]+)/i', $agent, $regs)) { $browser = 'Lynx'; $browser_ver = $regs[1]; } if (!empty($browser)) { Return addslashes($browser . ' ' . $browser_ver); } else { return 'Unknow browser'; } } |
Example
The code is as follows | Copy code |
function get_user_browser() $agent = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/MSIEs([^s|;]+)/i', $agent, $regs))
} function is_spider($record = true) //Determine whether it is a spider
if (empty($_SERVER['HTTP_USER_AGENT'])) return ''; $searchengine_bot = array( $searchengine_name = array( $spider = strtolower($_SERVER['HTTP_USER_AGENT']); //将用户浏览器信息转成小写 foreach ($searchengine_bot AS $key => $value) return $spider; $spider = ''; return ''; |