Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

browser & version detect

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

browser & version detect

Postby Fife Club » Tue Dec 10, 2002 11:21 am

Hi everybody. I was wondering if somebody could help me out. I don't know diddly squat about php except for echo and print and to always put a \ before a quote. I'm working on a page that absolutely needs absolute positioning (css) but I've learned that some browsers just screw it up so bad that I need a completely separate code to use for those browsers. What I'm looking for it the php code (or other language) that will determine the browser type and version and print different code depending on what it is. I've searched thru hundreds of old posts but either they're not quite what I need or there's 20 different ways to do it (javascript, browser.ini, get browser, etc) so I don't know which way to go.

What I need (in english) is:

If MSIE 5 or higher or NS6 or higher then "print this code"
// that's for the browsers that can handle my preferred code

Else "print alternate safe code"
// this is for lower versions or NS and IE that can't handle my preferred code including all alternate browsers (opera, etc) because I'm just don't want to make it that complicated.

(I suppose it's possible to list allowed browsers/versions in a variable but I don't know if that's necessary or not)

Like I said, I don't really know what I'm doing but I'd really appreciate it if somebody could help me out with the little snippet of code that will help me accomplish this task.

Thanks!!!!
User avatar
Fife Club
New php-forum User
New php-forum User
 
Posts: 15
Joined: Wed Oct 09, 2002 7:50 pm
Location: Charlotte, NC, USA

Postby webwired » Tue Dec 10, 2002 1:57 pm

Maybe something like

if ($HTTP_USER_AGENT==Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1))


and so on and so forth...
User avatar
webwired
New php-forum User
New php-forum User
 
Posts: 26
Joined: Sat Nov 23, 2002 5:20 pm
Location: St. Louis, Missouri

Postby webwired » Wed Dec 11, 2002 11:58 pm

I came across this while doing some research... I think it fits your bill... get_browser ... more info @ http://www.php.net/manual/en/printwn/fu ... rowser.php
User avatar
webwired
New php-forum User
New php-forum User
 
Posts: 26
Joined: Sat Nov 23, 2002 5:20 pm
Location: St. Louis, Missouri

Postby Fife Club » Thu Dec 12, 2002 6:36 am

Thanks. In total I tried 6 different code suggestions. Only two worked for my specific needs. Here are the two codes incase somebody else needs something like it:

Code: Select all
<?
$browser = strtolower($_SERVER["HTTP_USER_AGENT"]);
if (strstr($browser,"opera")) {
$agent = "op";
}
elseif (strstr($browser,"msie 6")) {
$agent = "ie6";
}
elseif (strstr($browser,"msie 5.5")) {
$agent = "ie55";
}
elseif (strstr($browser,"msie 5.01")) {
$agent = "ie501";
}
elseif (strstr($browser,"msie 5.0")) {
$agent = "ie50";
}
elseif (strstr($browser,"mozilla/4")) {
$agent = "ns4";
}
elseif (strstr($browser,"mozilla/5")) {
$agent = "ns6";
} else {
$agent = "standard";
}

echo $agent;


if ("ie6" == $agent || "ie55" == $agent || "ie501" == $agent || "ie50" == $agent || "ns6" == $agent)
{
   print "place tab code for good browsers";
}
else
{
   print "safe code for lower and unknown browsers";
}
?>



that one tested out well although I'll mention that I used an IE5.0 and it came back as 5.01 - but that's just a technicality that didn't matter.

and then this code

Code: Select all
<?
/****  Function to show browser specific things ****/

function Hello_browser(){ global $HTTP_USER_AGENT;
if(strstr($HTTP_USER_AGENT, "MSIE 6")) {
print "Hi! you have IE6";} // Or whatever...
else if(strstr($HTTP_USER_AGENT, "MSIE 5")){
print "Hi! you have IE5";}
else if(strstr($HTTP_USER_AGENT, "MSIE 4")){
print "Hi! you have IE4";}
else if(strstr($HTTP_USER_AGENT, "Mozilla/4")){
print "Hi! you have NS4";}
else if(strstr($HTTP_USER_AGENT, "Netscape6")){
print "Hi! you have NS6";}
else if(strstr($HTTP_USER_AGENT, "Mozilla/5")){
print "Hi! you have Mozilla";}
else if(strstr($HTTP_USER_AGENT, "Opera/6")){
print "Hi! you have Opera";}};

Hello_browser();
?>



This one works well but it doesn't distinguish decimals of browser versions - but that didn;t matter in my case either
User avatar
Fife Club
New php-forum User
New php-forum User
 
Posts: 15
Joined: Wed Oct 09, 2002 7:50 pm
Location: Charlotte, NC, USA


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.