Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 04:10 PM

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.
http://www.DaveLinger.com
dave at linger dot com

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 July 2006 - 04:17 PM

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

#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 04:23 PM

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{}
http://www.DaveLinger.com
dave at linger dot com

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 24 July 2006 - 04:35 PM

Use ||

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 July 2006 - 04:38 PM

This article here, shows how to use browser specific CSS with PHP. It shows how to correctly detect the user agent etc.

#6 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 24 July 2006 - 04:38 PM

Use ||


I would use a switch. (easyer to read and expand)
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#7 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 24 July 2006 - 04:40 PM

Just as a side note, IE6 does support full transparancy within png images, it doesn't support gradual transparency/opacity.

#8 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 04:41 PM

so I would say...


$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.";
}

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 =/
http://www.DaveLinger.com
dave at linger dot com

#9 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 24 July 2006 - 04:57 PM

Yeah... a switch would be easier. You got the OR syntax wrong anyway...

$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;
}


#10 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 04:58 PM

rofl firefocks makes me laffles.

Thanks!
http://www.DaveLinger.com
dave at linger dot com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users