1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Script To Redirect Users Based On Their Browser

Discussion in 'PHP' started by NeverTheSame, Apr 4, 2010.

  1. #1
    Wrote this for a site I'm making. Thought I'd share it here.

    <?php
    //******************************
    //********CONFIGURATION*********
    //******************************
    
    $MSIE_URL = "";
    $FIREFOX_URL = "";
    $OPERA_URL = "";
    $SAFARI_URL = "";
    $CHROME_URL = "";
    $OTHER_URL = "";
    
    //******************************
    //******************************
    
    // Redirection code
    $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
    
    function str_present($str,$substr)
    {
    $pos = strpos($str,$substr);
    
    if($pos === false) {
     return false;
    }
    else {
     return true;
    }
    }
    
    if (str_present($HTTP_USER_AGENT, "MSIE")) 
    {
    	Header ("Location: " . $MSIE_URL);
    }
    
    else if (str_present($HTTP_USER_AGENT, "Firefox"))
    { 
    	Header ("Location: " . $FIREFOX_URL);
    }
    	
    else if (str_present($HTTP_USER_AGENT, "Chrome"))
    { 
    	Header ("Location: " . $CHROME_URL);
    }
    	
    else if (str_present($HTTP_USER_AGENT, "Opera"))
    { 
    	Header ("Location: " . $OPERA_URL);
    }
    	
    else if (str_present($HTTP_USER_AGENT, "Safari"))
    { 
    	Header ("Location: " . $SAFARI_URL);
    }
    	
    else
    { 
    	Header ("Location: " . $OTHER_URL);
    }
    ?>
    PHP:

     
    Last edited: Apr 4, 2010
    NeverTheSame, Apr 4, 2010 IP
  2. Programmer2009

    Programmer2009 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Very nice addition.

    This would be great for many site's.
     
    Programmer2009, Apr 4, 2010 IP
  3. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your addition to this section however if i may suggest when you are planning on releasing code to the public it would be wise to have some kind of "Coding Standard and Naming Convention" as your braces looks randomly inserted and laid out.
     
    Brandon.Add.On, Apr 4, 2010 IP
  4. NeverTheSame

    NeverTheSame Peon

    Messages:
    94
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Fixed the braces, kind of.
     
    NeverTheSame, Apr 4, 2010 IP
  5. sbaldam1992

    sbaldam1992 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    what would be the user agent for the ipod touch and the iphone?
     
    sbaldam1992, Apr 4, 2010 IP
  6. Glock

    Glock Active Member

    Messages:
    274
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #6
    Thanks alot fr the post.. Had a lil issue wid ppl coming frm I.E. Will redirect them easily.
     
    Glock, Apr 4, 2010 IP
  7. eSpenders.com

    eSpenders.com Peon

    Messages:
    1,567
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #7
    very nice
    I have been debating on implementing this for some of my older sites that have issues with ie etc as id prefer users use better browsers :p
    Im currently using a old browser check that just display a big download firefox at the top of the page if ff is not detected but this would be much nicer as i can redirect ie & safari users to a custom download firefox page etc
    Thanks for the share
     
    eSpenders.com, Apr 4, 2010 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    I suggest you use stripos for case insensitivity. Sometimes the user agent is not in the case as expected.
     
    Kaizoku, Apr 4, 2010 IP
  9. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)
    Mozilla/5.0 (iPod; U; CPU like Mac OS X; en)

    Is what google suggests
     
    Rory M, Apr 4, 2010 IP
  10. sbaldam1992

    sbaldam1992 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    so i would use

    if (str_present($HTTP_USER_AGENT, "iPhone"))
    {

    or
    if (str_present($HTTP_USER_AGENT, "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) "))
    {
    ??
     
    sbaldam1992, Apr 4, 2010 IP
  11. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You should use the iPhone as it's more generic and doesn't depend on a specific version of mozilla.
     
    Brandon.Add.On, Apr 4, 2010 IP
  12. sbaldam1992

    sbaldam1992 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hi,
    this might just be me but i get

    Warning: Cannot modify header information - headers already sent by (output started at /home/xnalarac/public_html/drwho/index.php:1) in /home/xnalarac/public_html/drwho/index.php on line 104

    do i need to put this code at the top?
    sorry i am answering my own question because i just tried it and you do.
     
    sbaldam1992, Apr 4, 2010 IP
  13. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #13
    Your Safari line is likely going to catch iPod, iPhone, Droid Eris, and a few other devices that use Safari as their web browser.
     
    joebert, Apr 4, 2010 IP
  14. sbaldam1992

    sbaldam1992 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    It won't catch iPod or iPhone users as the user agent for the iPhone is iPhone, and the one for the iPod is iPod. I guess it would be the same with the droid and the other devices
     
    sbaldam1992, Apr 5, 2010 IP
  15. Cloud Computing Forum

    Cloud Computing Forum Guest

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Hi, thanks for posting your work. Can I suggest the big group of IF statements might be better if re-writen as a switch statement?
     
    Cloud Computing Forum, Apr 7, 2010 IP
  16. Cloud Computing Forum

    Cloud Computing Forum Guest

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Not tested but here is my version.

    
    <?php
    $USER_AGENT = explode('/', $_SERVER ['HTTP_USER_AGENT']);
    
    /* Eliminate IE */
    if( strpos($USER_AGENT[1], 'MSIE') !== false ){
    	header('Locaion: ' . $MSIE_URL);
    }
    
    /* Redirection */
    switch ($USER_AGENT[0]) {
    	
    	case 'Mozilla' : 
    		header('Locaion: *FIREFOX_URL*' );
    		break;
    	case 'Chrome' : 
    		header('Locaion: *CHROME_URL*');
    		break;
    	case 'Opera' : 
    		header('Locaion: *OPERA_URL*');
    		break;
    	case 'Googlebot' : 
    		header('Locaion: *GOOGLE_BOT_URL*');
    		break;
    	case 'msnbot' : 
    		header('Locaion: *MSN_BOT_URL*');
    		break;
    	case 'Safari' : 
    		header('Locaion: *SAFARI_URL*');
    		break;
    	default : 
    		header('Locaion: *DEFAULT_URL*');
    		break;
    } 
    PHP:
     
    Cloud Computing Forum, Apr 7, 2010 IP
    DomainerHelper likes this.
  17. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #17
    Here's an actual iPod user-agent string, taken directly from my raw server logs for an iPod visitor.

    Mozilla/5.0 (iPod; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16
    Code (markup):
    See how it has Safari towards the end there ? A simple "strpos" is going to match that user agent.

    Here's a user-agent for an iPhone, same situation here.

    Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16
    Code (markup):
    Again, here we have an Android powered phone, again the same situation with Safari.

    Mozilla/5.0 (Linux; U; Android 1.5; en-us; MB200 Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1
    Code (markup):
    What about these agents for Opera Mini ?
    This is a smartphone browser, but it's going to register as Opera with that script.

    Opera/8.01 (J2ME/MIDP; Opera Mini/3.1.10423/1724; en; U; ssr)
    Opera/9.80 (J2ME/MIDP; Opera Mini/5.0.18741/1114; U; id) Presto/2.4.15
    Code (markup):
    To make things even more interesting, Apple recently approved Opera Mini for use on iPhone/iPad/etc, so now you're going to have Opera Mini user-agent strings combined with elements from iPhoneOS.

    :)
     
    joebert, Apr 13, 2010 IP
  18. sbaldam1992

    sbaldam1992 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Heres the user-agent for the Opera Mini for the iPod/iPhone
    Opera/9.80 (iPhone; Opera Mini/5.0.0176/802; U; en) Presto/2.4.15
    Code (markup):
     
    sbaldam1992, Apr 24, 2010 IP
  19. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #19
    @Cloud Computing forum..

    Locaion: should be Location:
     
    danx10, Apr 24, 2010 IP
  20. Cloud Computing Forum

    Cloud Computing Forum Guest

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Well spotted! I'll update the post.

    
    <?php
    $USER_AGENT = explode('/', $_SERVER ['HTTP_USER_AGENT']);
    
    /* Eliminate IE */
    if( strpos($USER_AGENT[1], 'MSIE') !== false ){
        header('Location: *IE_URL*' );
    }
    
    /* Redirection */
    switch ($USER_AGENT[0]) {
       
        case 'Mozilla' :
            header('Location: *FIREFOX_URL*' );
            break;
        case 'Chrome' :
            header('Location: *CHROME_URL*');
            break;
        case 'Opera' :
            header('Location: *OPERA_URL*');
            break;
        case 'Googlebot' :
            header('Location: *GOOGLE_BOT_URL*');
            break;
        case 'msnbot' :
            header('Location: *MSN_BOT_URL*');
            break;
        case 'Safari' :
            header('Location: *SAFARI_URL*');
            break;
        default :
            header('Location: *DEFAULT_URL*');
            break;
    }
    
    Code (markup):
     
    Cloud Computing Forum, May 12, 2010 IP