Php : Finding Chrome and Safari Browsers

前端 未结 2 1294
夕颜
夕颜 2021-02-19 13:34

I use below code to find user agent,

    $user_agent = $_SERVER[\'HTTP_USER_AGENT\']; 
    if (preg_match(\'/MSIE/i\', $user_agent)) { 
       echo \"Internet          


        
相关标签:
2条回答
  • 2021-02-19 14:06

    Try this :

    $browser = get_browser(null, true);
    print_r($browser);
    

    From doc : Attempts to determine the capabilities of the user's browser, by looking up the browser's information in the browscap.ini file.

    ref: http://php.net/manual/en/function.get-browser.php

    0 讨论(0)
  • 2021-02-19 14:22

    Chrome's user agent contains Safari but Safari's user agent doesn't contain Chrome so use if ... elseif:

    if (stripos( $user_agent, 'Chrome') !== false)
    {
        echo "Google Chrome";
    }
    
    elseif (stripos( $user_agent, 'Safari') !== false)
    {
       echo "Safari";
    }
    

    Note: use stripos instead of strpos to account for case-variations.

    0 讨论(0)
提交回复
热议问题