Jump to content

Archived

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

DaveLinger

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

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
I'm not worried about people "fooling" it, because it'd be their own fault if the site looks like crap :D

how would I have multiple choices in the if statement?

like... if($_SERVER['HTTP_USER_AGENT'] == (any of multiple agents)){

}else{}

Share this post


Link to post
Share on other sites
This article [url=http://www.stylegala.com/articles/no_more_css_hacks.htm]here[/url], shows how to use browser specific CSS with PHP. It shows how to correctly detect the user agent etc.

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=101694.msg402674#msg402674 date=1153758938]
Use ||
[/quote]

I would use a switch. (easyer to read and expand)

Share this post


Link to post
Share on other sites
Just as a side note, IE6 does support full transparancy within png images, it doesn't support gradual transparency/opacity.

Share this post


Link to post
Share on other sites
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 =/

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.