Solved

Display different images based on the browser

Posted on 2004-08-29
8
205 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 …
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 dynamically set the form action using jQuery.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now