Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display different images based on the browser

Posted on 2004-08-29
8
Medium Priority
?
257 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 500 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
Suggested Courses

824 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