Hello,
I am trying my best to figure the browser detection code I found in the article on this site from 2001 or 02.. I think I have it updated to include Firefox which was the only major browser not listed.
What I am trying to do is use the browser detect script to determine which browser is being used for the sake of determining which stylesheet file to link to. I am looking for suggestions of a.) an easier method to do this, or b.) how to make where I am going with this work properly.
So, what I have done is put several <link>s to the different css files for which browser the user is using. I am painstakingly coding the stylsheets for each browser, then displaying them in the browser after updates to make sure they work. Here is what I have:
handle_form.php
<html>
<head>
<title>REAP</title>
<link rel="stylesheet" type="text/css" href="styles_ff.css" />
<link rel="stylesheet" type="text/css" href="styles_op.css" />
<link rel="stylesheet" type="text/css" href="styles_ns.css" />
<link rel="stylesheet" type="text/css" href="styles_ie.css" />
</head>
<body>
</head>
<body>
<?php
require ('browser.php');
browser.php
<?php
unset ($BROWSER_AGENT);
unset ($BROWSER_VER);
unset ($BROWSER_PLATFORM);
function browser_get_agent () {
global $BROWSER_AGENT;
return $BROWSER_AGENT;
}
function browser_get_version() {
global $BROWSER_VER;
return $BROWSER_VER;
}
function browser_get_platform() {
global $BROWSER_PLATFORM;
return $BROWSER_PLATFORM;
}
function browser_is_mac() {
if (browser_get_platform()=='Mac') {
return true;
} else {
return false;
}
}
function browser_is_windows() {
if (browser_get_platform()=='Win') {
return true;
} else {
return false;
}
}
function browser_is_ie() {
if (browser_get_agent()=='IE') {
return "Internet Explorer";
} else {
return false;
}
}
function browser_is_netscape() {
if (browser_get_agent()=='MOZILLA') {
return "Netscape";
} else {
return false;
}
}
function browser_is_firefox() { //added this
if (browser_get_agent()=='MOZILLA') {
return "Firefox";
} else {
return false;
}
}
function browser_is_opera() { //added this
if (browser_get_agent()=='OPERA') {
return "Opera";
} else {
return false;
}
}
/* Determine browser and version */
if (ereg( 'MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
$BROWSER_VER=$log_version[1];
$BROWSER_AGENT='IE';
} elseif (ereg( 'Opera ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
$BROWSER_VER=$log_version[1];
$BROWSER_AGENT='OPERA';
} elseif (ereg( 'Mozilla/([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
$BROWSER_VER=$log_version[1];
$BROWSER_AGENT='MOZILLA';
} elseif (ereg( 'FIREFOX ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], // personally added this code$log_version)) {
$BROWSER_VER=$log_version[1];
$BROWSER_AGENT='FIREFOX';
} else {
$BROWSER_VER=0;
$BROWSER_AGENT='OTHER';
}
/*
Determine platform
*/
if (strstr($_SERVER['HTTP_USER_AGENT'],'Win')) {
$BROWSER_PLATFORM='Win';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Mac')) {
$BROWSER_PLATFORM='Mac';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Linux')) {
$BROWSER_PLATFORM='Linux';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Unix')) {
$BROWSER_PLATFORM='Unix';
} else {
$BROWSER_PLATFORM='Other';
}
/* THIS BROWSER VERSION IS LESS THAN 5 DO WHAT YOU WANT
if($BROWSER_VER <= '5'){
echo "What ever you want the script to do here"; */
}
?>
I know the code works as I tested it, but how to get the code to work to determine which stylesheet to load for me is another story.
my suggestion
<?php
//something like this is what I had in mind
if ($BROWSER_AGENT == 'IE'){
<link rel="stylesheet" type="text/css" href="styles_ie.css" />; //obviously needing a variable assigned to it
} elseif ($BROWSER_AGENT == 'FIREFOX'){
<link rel="stylesheet" type="text/css" href="styles_ff.css" />;
}
?>
I just don't quite understand how the <head> tag works with php and if you can assign variables to it to change it etc. I hope this makes sense. Thanks for the help as you all are always quick and very helpful!
Warmaster