PHP $_server[' http_user_agent ' usage introduction

Source: Internet
Author: User
Tags ereg

In PHP http_user_agent is used to obtain information about the user, including the user's browser, operating system and other information,

The results shown are:

mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/53.0.2785.104 safari/537.36 core/1.53.2372.400 qqbrowser/ 9.5.10548.400

This time we can use regular expressions to match the above string, the user's browser, the operating system information

PHP According to the results of the match, the allocation of required CSS files, JS and so on

<?PHP//display the browser information of the access userEcho' Browser: '. Determinebrowser ($Agent) . ' <BR> ';//Show access to the user's operating system platformEcho' Platform: '. Determineplatform ($Agent). ' <BR> ';//a positive value expression that is better than parsing the string in $_server[' Http_user_agent '] gets access to the user's browser informationfunctionDeterminebrowser ($Agent) {$browseragent="";//Browser$browserversion="";//version of the browserif(Ereg(' MSIE ([0-9].[ 0-9]{1,2}) ',$Agent,$version)) { $browserversion=$version[1]; $browseragent= "Internet Explorer";} Else if(Ereg(' opera/([0-9]{1,2}.[ 0-9]{1,2}) ',$Agent,$version)) { $browserversion=$version[1]; $browseragent= "Opera";} Else if(Ereg(' firefox/([0-9.] {1,5}) ',$Agent,$version)) { $browserversion=$version[1]; $browseragent= "Firefox";}Else if(Ereg(' chrome/([0-9.] {1,3}) ',$Agent,$version)) { $browserversion=$version[1]; $browseragent= "Chrome";}Else if(Ereg(' safari/([0-9.] {1,3}) ',$Agent,$version)) { $browseragent= "Safari"; $browserversion="";}Else {$browserversion="";$browseragent= "Unknown";}return $browseragent." ".$browserversion;}//similarly get access to the user's browser informationfunctionDetermineplatform ($Agent) {$browserplatform= = "';if(Eregi(' Win ',$Agent) &&Strpos($Agent, ' 95 ')) {$browserplatform= "Windows 95";}ElseIf(Eregi(' Win 9x ',$Agent) &&Strpos($Agent, ' 4.90 ')) {$browserplatform= "Windows ME";}ElseIf(Eregi(' Win ',$Agent) &&Ereg(' 98 ',$Agent)) {$browserplatform= "Windows 98";}ElseIf(Eregi(' Win ',$Agent) &&Eregi(' NT 5.0 ',$Agent)) {$browserplatform= "Windows 2000";}ElseIf(Eregi(' Win ',$Agent) &&Eregi(' NT 5.1 ',$Agent)) {$browserplatform= "Windows XP";}ElseIf(Eregi(' Win ',$Agent) &&Eregi(' NT 6.0 ',$Agent)) {$browserplatform= "Windows Vista";}ElseIf(Eregi(' Win ',$Agent) &&Eregi(' NT 6.1 ',$Agent)) {$browserplatform= "Windows 7";}ElseIf(Eregi(' Win ',$Agent) &&Ereg(' 32 ',$Agent)) {$browserplatform= "Windows 32";}ElseIf(Eregi(' Win ',$Agent) &&Eregi(' NT ',$Agent)) {$browserplatform= "Windows NT";}ElseIf(Eregi(' Mac OS ',$Agent)) {$browserplatform= "Mac OS";}ElseIf(Eregi(' Linux ',$Agent)) {$browserplatform= "Linux";}ElseIf(Eregi(' Unix ',$Agent)) {$browserplatform= "Unix";}ElseIf(Eregi(' Sun ',$Agent) &&Eregi(' OS ',$Agent)) {$browserplatform= "SunOS";}ElseIf(Eregi(' IBM ',$Agent) &&Eregi(' OS ',$Agent)) {$browserplatform= "IBM Os/2";}ElseIf(Eregi(' Mac ',$Agent) &&Eregi(' PC ',$Agent)) {$browserplatform= "Macintosh";}ElseIf(Eregi(' PowerPC ',$Agent)) {$browserplatform= "PowerPC";}ElseIf(Eregi(' AIX ',$Agent)) {$browserplatform= "AIX";}ElseIf(Eregi(' HPUX ',$Agent)) {$browserplatform= "HPUX";}ElseIf(Eregi(' NetBSD ',$Agent)) {$browserplatform= "NetBSD";}ElseIf(Eregi(' BSD ',$Agent)) {$browserplatform= "BSD";}ElseIf(Ereg(' OSF1 ',$Agent)) {$browserplatform= "OSF1";}ElseIf(Ereg(' IRIX ',$Agent)) {$browserplatform= "IRIX";}ElseIf(Eregi(' FreeBSD ',$Agent)) {$browserplatform= "FreeBSD";}if($browserplatform= = ") {$browserplatform= "Unknown"; }return $browserplatform;}?>

The browser type is determined mainly by analyzing the content of _server["Http_user_agent", while the analysis browser language is the analysis _server["Http_accept_language".

Generally we can determine whether the user is currently open in the end can be judged as follows

  Private functionIs_wx_pro () {$user _agent=$_server[' Http_user_agent ']; if(Strpos($user _agent, ' micromessenger ') = = =false) {         //non-browser prohibited browsing         return false; } Else {         //browser, allowing access         Preg_match(‘/.*? (micromessenger\/([0-9.] +)) \s*/',$user _agent,$matches);//echo "Micromessenger";         Echo' <br>version: '.$matches[2];//Get version number         return true; }   }

PHP $_server[' http_user_agent '] usage introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.