Solved

Display different images based on the browser

Posted on 2004-08-29
8
236 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check mysql insert 12 39
issue with DB import 1 36
warning code: class php incomplete message 6 27
PHP $_POST vs asp request 4 22
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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

735 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