PHP Detect Browser?

liunx

Guest
PHP4+, register globals off

my php page looks fine in IE. it is NOT fine in netscape. what i would like to do is somehow use php to detect when the browser is netscape and render a page specifically for netscape.


for instance:
if i had two pages, originalhomepage.php and alteredhomepage.php, and if netscape is detected then it would call up alteredhomepage.php,

but

if netscape is NOT detected then it would simply call up originalhomepage.php

is this reaching? can this be done?This can't be done in PHP as this language is server side. You're going to want to get some client side stuff in like javascript :)

Here's something that I got together for you (place below code in <head></head> section)

<script language="Javascript" type='text/Javascript'>
<!--

if (navigator.appName=="Microsoft Internet Explorer")
window.location = "internetexplorer.php";
else if (navigator.appName=="Netscape")
window.location = "netscape.php";

//-->
</script>

Pretty basic at the moment (you can edit it to accept different browser versions).

In pseudo code if the browser is IE, then redirect to internetexplorer.php and if it's netscape, redirect to netscape.php

btw.. the reason I put up this javascript is because the process you want to achieve cannot be done in PHP (server side).. you need client side.Jolly why can't it? you can use php to detect which browser they have very easily.


<?php
$br = strtolower($_SERVER['HTTP_USER_AGENT']); // what browser they use.

if(ereg("msie", $br)) {
header("location: originalhomepage.php");
} else {
header("location: alteredhomepage.php");
}
?>

put that at the very top of you index.php page or whatever. this is very basic and can get very complicated. also remember that anybody can change the way there browser sends the info you are trying to detect. they can take the msie or netscape out so it is not fool proof.thank you both so much for taking the time to respond!

scoutt, when you said:
"also remember that anybody can change the way there browser sends the info you are trying to detect. they can take the msie or netscape out so it is not fool proof."

why would someone want to do this?

jolly factory, is there any big disadvantage to using the javascript method? the only thing i can think of is that the visitor could have it disabled on their computer, but i would think most wouldn't, right?

thanks in advancethis is the code that i use. as yu can see, i use it to swap certain elements of CSS to make up for the different ways browsers handle alpha-channel transparency. i also use it to produce a variable which i then use to display which version the user is seeing (top right of screen). you can easily edit it to do something else. it picks up the majority of browser/OS combos, though certainly not every possible configuration. see it in action at <!-- w --><a class="postlink" href="http://www.transmothra.com">www.transmothra.com</a><!-- w --><?php
/*
* $browser will contain one of the following values:
* 'iewin' : IE 4+ for Windows
* 'iemac' : IE 4 for Macintosh
* 'ie5mac' : IE 5 Macintosh
* 'nswin' : Netscape 4.x Windows
* 'nsunix' : Netscape 4.x Unix
* 'nsmac' : Netscape 4.x Mac
* 'ns6' : Netscape 6 / Mozilla
*/

function inAgent($agent) {
global $HTTP_USER_AGENT;
return (strpos($HTTP_USER_AGENT, $agent) === false) ? false : true;
}

switch (inAgent('MSIE')) {
case true:
if ( inAgent('Mac') ) {
$browser = inAgent('MSIE 5') ? 'ie5mac' : 'ie4mac';
} elseif ( inAgent('Win') ) {
$browser = 'iewin';
}
break;

case false:
if (inAgent('Mozilla/5')) {
$browser = 'ns6';
} elseif (inAgent('Mozilla/4')) {
if ( inAgent('Mac')) {
$browser = 'nsmac';
} else {
$browser = (inAgent('Win')) ? 'nswin' : 'nsunix';
}
} else {
$browser = "unknown";
}
break;
}

switch ($browser) {
case 'iewin':
$transluc_css = "background-image:\"alpha_white.png\";filter:\"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://www.htmlforums.com/transmothra.com/alpha_white.png', sizingMethod='scale')\";";
$sitever = "IE/Win version";
break;
case 'iemac':
$transluc_css = "background:\"#443355\";";
$sitever = "IE/Mac version";
break;
case 'ie5mac':
$transluc_css = "background:url(\"alpha_white.png\");";
$sitever = "IE5/Mac version";
break;
case 'nswin':
$transluc_css = "background:url(\"alpha_gray_2x2.gif\");";
$sitever = "NS/Win version";
break;
case 'nsunix':
$transluc_css = "background:url(\"alpha_gray_2x2.gif\");";
$sitever = "NS/Unix version";
break;
case 'nsmac':
$transluc_css = "background:url(\"alpha_gray_2x2.gif\");";
$sitever = "NS/Mac version";
break;
case 'ns6':
$transluc_css = "background:url(\"alpha_white.png\");";
$sitever = "NN6+ version";
break;
default:
$transluc_css = "background:\"#443355\";";
$sitever = "try <a href=http://www.htmlforums.com/archive/index.php/\"http://www.microsoft.com/ie/\" target=\"_blank\">IE</a> or <a href=\"http://browsers.netscape.com/\" target=\"_blank\">NN</a> version";
break;
}
?>Originally posted by shim
thank you both so much for taking the time to respond!

scoutt, when you said:
"also remember that anybody can change the way there browser sends the info you are trying to detect. they can take the msie or netscape out so it is not fool proof."

why would someone want to do this?

jolly factory, is there any big disadvantage to using the javascript method? the only thing i can think of is that the visitor could have it disabled on their computer, but i would think most wouldn't, right?

thanks in advance i'd have to say that most likely more people won't bother to fiddle with their browser settings to spoof another browser than will turn off JavaScripting. anywhere from 10-20% of your audience will have J/S turned off, depending on who your audience actually is. i personally find that the PHP sniffer does the job much better than J/S would, for that reason.

then again, i'm a total PHP zealot :POriginally posted by shim
thank you both so much for taking the time to respond!

scoutt, when you said:
"also remember that anybody can change the way there browser sends the info you are trying to detect. they can take the msie or netscape out so it is not fool proof."

why would someone want to do this?

jolly factory, is there any big disadvantage to using the javascript method? the only thing i can think of is that the visitor could have it disabled on their computer, but i would think most wouldn't, right?

thanks in advance
people change it becasue they can. no particular reason just because. I have seen some on my stats script so it is like 20% of people do, nothing to worry about.

and yeah javascript people is around 10% as well, but I wouldn't worry about that either.thanks all, i'm glad i found this site:rocker:Originally posted by scoutt
Jolly why can't it? you can use php to detect which browser they have very easily.


<?php
$br = strtolower($_SERVER['HTTP_USER_AGENT']); // what browser they use.

if(ereg("msie", $br)) {
header("location: originalhomepage.php");
} else {
header("location: alteredhomepage.php");
}
?>

put that at the very top of you index.php page or whatever. this is very basic and can get very complicated. also remember that anybody can change the way there browser sends the info you are trying to detect. they can take the msie or netscape out so it is not fool proof.

sorry scoutt.. I just assumed you couldn't in PHP. Thanks for setting me straight :)
 
Back
Top