Solved

Display different images based on the browser

Posted on 2004-08-29
8
221 Views
Last Modified: 2006-11-17
I'm sure this is an easy qusetion for anything who knows anything about PHP... but, I don't.

I want a PHP script that will display a different image (ie.png, opera.png, firefox.png etc..), depending on what browser the visitor is using.
I want the script to be able to identify 5 different browsers: IE, Firefox, Mozilla, Safari and Opera and I want an alternative image if the user is not using either of those 5 browsers.

By the way opera has a habit of spoofing itself as MSIE, but the user-agent string always contains the word opera somewhere.

This page has something similiar to the Javascript code that I was using:
http://www.quirksmode.org/js/detect.html
0
Comment
Question by:porcupine
  • 5
  • 3
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926304
You have to test User Agent environment variable:

$ua=getenv("HTTP_USER_AGENT");
0
 
LVL 1

Author Comment

by:porcupine
ID: 11926320
Yeah I know that, but I'm completely new to PHP and I don't know how to go about actually making the script.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926337
Something like:

$ua = strtoupper(getenv("HTTP_USER_AGENT"));

if (strstr($ua,"MSIE")) {
   $image = "ie.png";
} elseif (strstr($ua,"MOZILLA") {
   $image = "mozilla.png";
} elseif (strstr($ua,"OPERA") {

// etcetera
} else {
   $image = "other.png";
}

echo ("<IMG SRC='".$image."'>");
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926351
Sorry, missing some parenthesis:

<?php
$ua = strtoupper(getenv("HTTP_USER_AGENT"));

if (strstr($ua,"MSIE")) {
   $image = "ie.png";
} elseif (strstr($ua,"MOZILLA")) {
   $image = "mozilla.png";
} elseif (strstr($ua,"OPERA")) {

// etcetera
} else {
   $image = "other.png";
}

echo ("<IMG SRC='".$image."'>");


?>


Just add correct search strings.
0
 
LVL 1

Author Comment

by:porcupine
ID: 11926499
The problem with the above code is that for example the Opera string is:
MOZILLA/4.0 (COMPATIBLE; MSIE 6.0; WINDOWS NT 5.1) OPERA 7.51 [EN]

Which contains "MOZILLA", "MSIE" and "OPERA"

So in Opera the image displayed is not correct.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926510
Just evaluate Opera first, IE second and Mozilla third.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 11926532
Try with:

if (strstr($ua,"OPERA")) {
   $image = "opera.png";
} elseif (strstr($ua,"MSIE")) {
   $image = "msie.png";
} elseif (strstr($ua,"NETSCAPE")) {
   $image = "netscape.png";
} elseif (strstr($ua,"MOZILLA")) {
   $image = "mozilla.png";
} else {
   $image = "other.png";
}

also you can use regex but will be a bit messy
0
 
LVL 1

Author Comment

by:porcupine
ID: 11926597
Yeah, that works perfectly, Thank you.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question