Jump to content

if browser=foo, echo bar, else echo...


DaveLinger

Recommended Posts

so here's the deal. IE6 and below don't support PNG transparency, and VERY few people use IE7 so far, so I'd like to make an if statement that will echo certain code for IE users and different code for firefocks users.

like...

if([browser variable] = firefox){

}else{

}

but I dont know how to retrieve the user's browser and I don't know exactly what to check if it's equal to... like "mozilla firefox 1.5", or is it "ff1.5" or "mozilla1.5", I dont know.
Link to comment
https://forums.phpfreaks.com/topic/15505-if-browserfoo-echo-bar-else-echo/
Share on other sites

To get the user users agent (the browser) you use $_SERVER['HTTP_USER_AGENT']
You shouldn't rely on this variable too much as this can be fooled and some browsers dont send this, or disguise themeselfs.
The following is what is produced:
IE7/Win:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Opera9/Win:
Opera/9.00 (Windows NT 5.1; U; en)
FF1.5/Win:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4
so I would say...

[code]

$firefocks = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4";
$IE7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
$Opera = "Opera/9.00 (Windows NT 5.1; U; en)";

if($_SERVER['HTTP_USER_AGENT'] == "$firefocks"|"$IE7"|"$Opera"){
echo "You have a decent web browser"
}else{
echo "omgx your browza is t3h suxx0rz.";
}
[/code]

That should work? (or a switch I guess)

@SemiApocalyptic: If it only supports full transparency I might as well be using a gif in my case =/
Yeah... a switch would be easier. You got the OR syntax wrong anyway...

[code=php:0]
$firefocks = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4";
$IE7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
$Opera = "Opera/9.00 (Windows NT 5.1; U; en)";

switch ($_SERVER['HTTP_USER_AGENT']) {
  case $firefocks:
  case $IE7:
  case $Opera:
    echo "You have a decent web browser";
    break;
  default:
    echo "omgx your browza is t3h suxx0rz.";
    break;
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.