1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

NEED QUICK PHP HELP: How can I fix this code?

Discussion in 'PHP & Perl' started by Indiigo007, Apr 13, 2012.

  1. Indiigo007

    Indiigo007 Junior Member

    Joined:
    Nov 11, 2009
    Messages:
    189
    Likes Received:
    191
    Location:
    Los Angeles
    Hi, I have the following code to detect user agent:

    Code:
    <?php
    
    function agent($browser) {
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    return strstr($useragent,$browser);
    }
    
    
    if(agent("Firefox") != FALSE) {
    header('Location:http://location1.com');
    }
    else if(agent("Opera") != FALSE) {
    header('Location:http://location2.com');
    }
    else if(agent("Safari") != FALSE) {
    header('Location:http://location3.com');
    }
    else if(agent("MSIE") != FALSE) {
    header('Location:http://location4.com');
    }
    else {
    header('Location:http://location5.com');
    }
    ?>
    However, it thinks Chrome is "Safari." How do I modify this (or add to this) so it distinguishes Chrome from Safari?

    Any help would be much appreciated.
     
  2. trevorhoang

    trevorhoang Power Member

    Joined:
    Mar 28, 2011
    Messages:
    712
    Likes Received:
    114
    Occupation:
    manager
    Location:
    canada
    ugh! looking at that dizzies me. lol. sry i just had to comment. i hate php code stuff. hopefully others can help you
     
    • Thanks Thanks x 1
  3. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    Code:
    <?php
    
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    
    $firefox_url = 'http://location1.com/';
    $opera_url   = 'http://location2.com/';
    $safari_url  = 'http://location3.com/';
    $msie_url    = 'http://location4.com/';
    $chrome_url  = 'http://location5.com/';
    $default_url = 'http://location6.com/';
    
    if(preg_match('/firefox/i', $user_agent)) {
    	header("Location: $firefox_url", true, 301);
    	exit;
    
    } elseif(preg_match('/opera/i', $user_agent)) {
    	header("Location: $opera_url", true, 301);
    	exit;
    
    } elseif(preg_match('/safari/i', $user_agent)) {
    	header("Location: $safari_url", true, 301);
    	exit;
    
    } elseif(preg_match('/msie/i', $user_agent)) {
    	header("Location: $msie_url", true, 301);
    	exit;
    
    } elseif(preg_match('/chrome/i', $user_agent)) {
    	header("Location: $chrome_url", true, 301);
    	exit;
    
    } else {
    	header("Location: $default_url", true, 301);
    	exit;
    
    }
    
    ?>
    
    
     
    • Thanks Thanks x 1
  4. Indiigo007

    Indiigo007 Junior Member

    Joined:
    Nov 11, 2009
    Messages:
    189
    Likes Received:
    191
    Location:
    Los Angeles
    Thank you!
     
  5. sbchen

    sbchen Newbie

    Joined:
    May 3, 2012
    Messages:
    16
    Likes Received:
    0
    Chrome is Safari based, this is why you saw it like that..