Forum Moderators: coopster

Message Too Old, No Replies

Showing different things depending on browser

         

jozomannen

10:45 am on Sep 15, 2005 (gmt 0)

10+ Year Member



I want to show an image for IE-users and no image for all other users, how can I do that in PHP? I guess that I should use the useragent, but there is very many different types of useragents so I though maybe someone knows a premade script or function for this. Or maybe there is some really simple way. Please help.

Mike12345

11:27 am on Sep 15, 2005 (gmt 0)

10+ Year Member



I dont know of any pre-made script but im sure there is one. If you were writing your own, you could do something loosly following this logic:

if user-agent contains the letters IE, in uppercase and in that order, then img = IE.gif else img = notIE.gif

So basically my message is you dont have to work out each available user agent you can just set an image for one and a default one for everything else. The code would be dead simple. Although some browsers give the user the option to send user agent as though it were IE like opera, so that is something you need to consider. It depends on how important showing this image is to you.

jackvull

12:00 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



You could try getting this variable $_SERVER[HTTP_USER_AGENT] and then use a few if statements to check for the browser I guess...

There might also be some javascript available to do this...

Mike12345

12:03 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



ok had a quick fiddle you could do this:

<?php

$a=$_SERVER['HTTP_USER_AGENT'];

if (preg_match('/IE/', $_SERVER['HTTP_USER_AGENT'])) {
$img="IE.gif";
}
else
{$img="notIE.gif";}

?>

then call in your img:

<img src="<?php echo $img?>">

This code is probably messy but ive testede it and it works for me.

King_Kong

12:05 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



This page has some examples in the comments..

[php.net...]

jozomannen

1:51 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



I haven't tested the get_browser function yet, but I will soon. I just thought about code where it checks it the useragent contains IE. Then it would show the IE.gif for this useragent:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.0

Mike12345

3:11 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



Give this a whirl:

<?php

$a=$_SERVER['HTTP_USER_AGENT'];

if (preg_match('/IE/', $_SERVER['HTTP_USER_AGENT']) && (preg_match('/Opera/', $_SERVER['HTTP_USER_AGENT']))) {
$img="notIE.gif";
}
else
if (preg_match('/IE/', $_SERVER['HTTP_USER_AGENT'])) {
$img="IE.gif";
}
else
{$img="notIE.gif";}

?>

However with a script like this you would have to keep writing conditions everytime you find a browser that can spoof its user agent. This would be the same for anything taking its information from the http_user_agent environment variable.

GaryK

3:33 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use get_browser() with the browscap.ini option enabled then you will be able to identify the browser by name. PHP has a browscap.ini supplier they recommend in their online manual. With the exception of spoofed user agents this file is able to distinguish between browsers like Opera and others which include IE (or MSIE) as part of the user agent.

jozomannen

4:33 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



Ok, I'll check that out, but it's a pretty smal site and it's only an image with a bookmark-link (that function only works for IE). So with this script where it excludes Opera, I guess it will work good for 99,9% of the visitors, so I'm pleased =)

Thanks a lot for the help.

grandpa

4:35 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I use a class called phpsniff (Search for that). It accurately returns browser types, os, and more.

jozomannen

6:08 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



That sounded interesting for another site of mine. I think I will stick to that simple one that just checks the useragent if IE is in there.

Thanks for the help everyone!