Chrome and Safari not playing nice. Safari identified when Chrome used.

Discussion in 'PHP' started by Matt Ridge, Nov 29, 2011.

  1. #1
    Chrome is the browser I use, I have it set up so that the script detects safari and chrome as separate entities. When I browse to it, it shows:

    You are using Windows XP with a Chrome Web Browserwith a Safari Web Browser

    now I know stristr() is a case-insensitive version of strstr(), which will find the first form of a word in a string. The problem is that when I use it, the rest of the script doesn't work... it goes to the part as if nothing is there, and shows the text on the bottom. I can't figure out why. Can someone help me, is there a chrome/safari level of priority or am I just missing something?

    
    <?php
    //Booleans to set OS and Browser to False.
    $os = false;
    $browser = false;
    //Booleans for Web Browser & OS Functions.
    $xp = xp();
    $vista = vista();
    $win7 = win7();
    $ubuntu = ubuntu();
    $chrome = chrome();
    $safari = safari();
    $firefox = firefox();
    $ie9 = ie9();
    $ie8 = ie8();
    
    
    //Operating Systems
    function xp(){return(preg_match("/Windows NT 5.1/", $_SERVER['HTTP_USER_AGENT']));}
    function vista(){return(preg_match("/Windows NT 6.0/", $_SERVER['HTTP_USER_AGENT']));}
    function win7(){return(preg_match("/Windows NT 6.1/", $_SERVER['HTTP_USER_AGENT']));}
    function ubuntu(){return(preg_match("/Ubuntu/", $_SERVER['HTTP_USER_AGENT']));}
    
    
    //Web Browsers
    function chrome(){return(preg_match("/Chrome/", $_SERVER['HTTP_USER_AGENT']));}
    function safari(){return(preg_match("/Safari/", $_SERVER['HTTP_USER_AGENT']));}
    function firefox(){return(preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT']));}
    function ie9(){return(preg_match("/MSIE 9.0/", $_SERVER['HTTP_USER_AGENT']));}
    function ie8(){return(preg_match("/MSIE 8.0/", $_SERVER['HTTP_USER_AGENT']));}
    
    
    // If you have one of the valid Operating System.
    if($ubuntu){echo 'You are using Ubuntu Operating System ';}
    if($xp){echo 'You are using Windows XP ';}
    if($vista){echo 'You are using Windows Vista Operating System ';}
    if($win7){echo 'You are using Windows 7 Operating System ';}
    
    
    
    
    // If you have one of the valid Web Browser.
    if($chrome){echo 'with a Chrome Web Browser';}
    if($firefox){echo 'with an Firefox Web Browser';}
    if($safari){echo 'with a Safari Web Browser';}
    if($ie8){echo 'with an Internet Explorer 8 Web Browser';}
    if($ie9){echo 'with an Internet Explorer 9 Web Browser';}
    
    
    //If OS or Browser not found in list.
    if ($ubuntu || $xp || $vista || $win7)
    $os = true;
    
    
    if($firefox || $chrome || $safari || $ie9 || $ie8)
    $browser = true;
    
    
    if(!$browser || !$os){
    
    
    echo'<strong>';
    echo '<br />' . $_SERVER['HTTP_USER_AGENT'] . '<br /><br />Administrator someone in your work force is using an unsupported browser or OS, please email this information to the developer of the NCMR software you are using. It will allow your browser/OS combination  to be used correctly. Sorry for the inconvenience.</strong> <br /><br />Please copy and paste the text above and send it to your web administrator. It will explain everything he/she needs to do.<br />';}
    
    
    ?>
    Code (markup):
     
    Matt Ridge, Nov 29, 2011 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    The User Agent string in Chrome includes the word "Safari". To differentiate them, you'll want to use 'else if' in your list of 'if' statements so that once a match is found, the rest of the tests won't be tried. You'll probably have to experiment a bit to get it right, but it shouldn't be too hard. Good luck!
     
    rainborick, Nov 29, 2011 IP
  3. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Not really a possibility... the program I'm designing will utilize the different browsers and OS systems to produce a specific format for not only a website, but also the CSS that it uses. I need to have the script show Safari and Chrome separately.

    Hence why I asked how to use the stristr()command in this situation.
     
    Matt Ridge, Nov 29, 2011 IP
  4. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #4
    Wouldn't something like this work?

    function safari(){return(preg_match("/Safari/", $_SERVER['HTTP_USER_AGENT']));}
    function chrome(){ return(preg_match("/Chrome/", $_SERVER['HTTP_USER_AGENT']) && !safari());}
    
    PHP:
    The problem as I got it is that both Safari and Chrome have a "Chrome" string on the user-agent information. But Chrome doesn't have "Safari" on its user-agent information, am I right?
     
    proactiv3, Nov 29, 2011 IP
  5. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Yup, you are right, but the string you suggested will remove the Chrome attribute all together, because safari is in the chrome attribute...
     
    Matt Ridge, Nov 30, 2011 IP
  6. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Solved it.

    
    <?php
    //Booleans to set OS and Browser to False.
    $os = false;
    $browser = false;
    //Booleans for Web Browser & OS Functions.
    $info = $_SERVER['HTTP_USER_AGENT'];
    $xp = 'Windows NT 5.1';
    $vista = 'Windows NT 6.0';
    $win7 = 'Windows NT 6.1';
    $ubuntu = 'Ubuntu';
    $ie9 = 'ie9';
    $ie8 = 'ie8';
    $chrome = '/Chrome/';
    $safari = '/Safari/';
    $firefox = '/Firefox/';
    
    
    
    
    //Operating Systems
    if (stristr($info, "Windows NT 5.1")) {echo 'You are using a Windows XP Operating System ';}
    if (stristr($info, "Windows NT 6.0")) {echo 'You are using a Windows Vista Operating System ';}
    if (stristr($info, "Windows NT 6.1")) {echo 'You are using a Windows 7 Operating System ';}
    if (stristr($info, "Ubuntu")) {echo 'You are using an Ubuntu Operating System ';}
    if (stristr($info, "Mac OS")) {echo 'You are using a Macintosh Operating System ';}
    
    
    
    
    //Web Browsers
    if (stristr($info, "Chrome") !== FALSE) {stristr($info,"Safari");       
    		$chrome = 'Chrome';
    			echo 'with a Chrome Web Browser ';}
    elseif (stristr($info, "Safari")) {echo 'with a Safari Web Browser ';}
    
    
    if (stristr($info, "Firefox")) {echo 'with a Firefox Web Browser ';}
    
    
    
    
    
    
    
    
    //If OS or Browser not found in list.
    if ($ubuntu || $xp || $vista || $win7)
    $os = true;
    
    
    if($firefox || $chrome || $safari || $ie9 || $ie8)
    $browser = true;
    
    
    if(!$browser || !$os){
    
    
    echo'<strong>';
    echo '<br />' . $_SERVER['HTTP_USER_AGENT'] . '<br /><br />Administrator someone in your work force is using an unsupported browser or OS, please email this information to the developer of the NCMR software you are using. It will allow your browser/OS combination  to be used correctly. Sorry for the inconvenience.</strong> <br /><br />Please copy and paste the text above and send it to your web administrator. It will explain everything he/she needs to do.<br />';}
    
    
    ?>
    Code (markup):
     
    Last edited: Nov 30, 2011
    Matt Ridge, Nov 30, 2011 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    
    if (stristr($info, "Chrome") !== FALSE) {
      //I'm curious as to what the next line is supposed to accomplish
      stristr($info,"Safari");       
      $chrome = 'Chrome';
      echo 'with a Chrome Web Browser ';
    }
    
    PHP:
     
    Rukbat, Dec 1, 2011 IP