Browser Sniffing >> Insert Browser Specific HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
M4X
Forum Newbie
Posts: 2
Joined: Mon Sep 13, 2010 8:10 pm

Browser Sniffing >> Insert Browser Specific HTML

Post by M4X »

Hi, i'm trying to create a test that detects if a user is browsing with firefox. If the user is browsing with firefox i want the favicon to be directed to a animated gif. If the are not using firefox i want the favicon to directed to a standard non animated png favicon. I have researched around to try and find a solution using php but i'm very new to php and can't figure it out. if someone could give me a hand or point me in the right direction it would be much appreciated. My usual area is only HTML and CSS. Is php the best way to achieve this or should i be using another method eg. javascript. Thanks a lot! This is some code i have found and modified which doesn't work at all...

Code: Select all

<?php $browser = $_SERVER['HTTP_USER_AGENT']; ?>  

<?php if (strstr($browser, "Firefox")) { ?>  

<link rel="icon" href="address/favicon.gif" type="image/gif">

<?php }else{ ?>  

<link rel="icon" href="address/favicon.png" type="image/png">

<?php } ?>
Last edited by M4X on Mon Sep 13, 2010 10:07 pm, edited 2 times in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Browser Sniffing >> Insert Browser Specific HTML

Post by Jonah Bron »

Try this:

Code: Select all

<?php
if (stripos($_SERVER['HTTP_USER_AGENT'], 'firefox') === false) {
?>
<link rel="icon" href="address/favicon.png" type="image/png">
<?php
} else {
?>
<link rel="icon" href="address/favicon.gif" type="image/gif">
<?php
}
?>
Last edited by Jonah Bron on Mon Sep 13, 2010 9:52 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Browser Sniffing >> Insert Browser Specific HTML

Post by requinix »

stripos returns false, not -1.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Browser Sniffing >> Insert Browser Specific HTML

Post by Jonah Bron »

Oh yeah :oops:I got it mixed up with JavaScript's indexOf() :mrgreen:

Fixed.
M4X
Forum Newbie
Posts: 2
Joined: Mon Sep 13, 2010 8:10 pm

Re: Browser Sniffing >> Insert Browser Specific HTML

Post by M4X »

LEGENDS, works perfectly! Thanks so much!
Post Reply