Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display different images based on the browser

Posted on 2004-08-29
8
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

722 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