Browser Ditection code in PHP?

Discussion in 'PHP' started by Abhik, Dec 9, 2007.

  1. #1
    to be frank, I want to put firefox referral in my forum running on vBulletin.
    Is there any code which will help me to show it in all browsers except Firefox?

    any help will be appreciated.

    Thanks in advance.
     
    Abhik, Dec 9, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    if (!preg_match('~Firefox~i', $_SERVER['HTTP_USER_AGENT']))
    {
        // Show link
    }
    
    PHP:
     
    nico_swd, Dec 9, 2007 IP
  3. Abhik

    Abhik ..:: The ONE ::..

    Messages:
    11,337
    Likes Received:
    606
    Best Answers:
    0
    Trophy Points:
    410
    Digital Goods:
    2
    #3
    Man.. I am getting this error while saving the template..
    Please suggest something.
     
    Abhik, Dec 9, 2007 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Try one of my custom functions

    
    // Usage
    $browser = detect();
    
    if ($browser != "FIREFOX") {
    	// Show Firefox referral
    }
    
    function detect() {
    	$browsers = array("IE","OPERA","MOZILLA","NETSCAPE","FIREFOX","SAFARI");
    	$browser = "OTHER";
    	foreach ($browsers as $parent) {
    		$s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent);
    		if ($s) $browser = $parent;
    	}
    	return $browser;
    }
    
    PHP:
     
    Kaizoku, Dec 9, 2007 IP
  5. Paul Starsky

    Paul Starsky Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    There is a function for that in php get_browser()

    
    echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
    
    $browser = get_browser(null, true);
    print_r($browser);
    
    PHP:
     
    Paul Starsky, Dec 10, 2007 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    Never knew that function exists o.0
     
    Kaizoku, Dec 10, 2007 IP
  7. Abhik

    Abhik ..:: The ONE ::..

    Messages:
    11,337
    Likes Received:
    606
    Best Answers:
    0
    Trophy Points:
    410
    Digital Goods:
    2
    #7
    Thanks, but, none of them are working.. I am getting the same error each time.
     
    Abhik, Dec 10, 2007 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    I suggest you fix your original problem first... we can't help you without seeing your whole script.
     
    Kaizoku, Dec 10, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Yes... the error is in your code, not ours. You will have to post it.

    Actually... are you putting this code in your template? This won't work... if you're working with vBulletin, open for example global.php. and place this line somewhere, where it doesn't damage other code.
    
    $show['firefox_link'] = !preg_match('~Firefox~i', $_SERVER['HTTP_USER_AGENT']);
    
    PHP:
    And in your template:
    
    <if condition="$show[firefox_link']">
        <!-- link here //-->
    </if>
    
    Code (markup):
     
    nico_swd, Dec 10, 2007 IP