Jump to content
Killersites Forums

PHP to Detect Browser and Operating System


shelfimage
 Share

Recommended Posts



Tests for browsers and operating systems




<?php
if ($ua) {

// ---- Test if using a Handheld Device ----
if ($android) { // Android
echo 'You are using an Android!
';
}
if ($blackbery) { // Blackbery
echo 'You are using a Blackbery!
';
}
if ($iphone) { // iPhone
echo 'You are using an iPhone!
';
}
if ($palm) { // Palm
echo 'You are using a Palm!
';
}

if ($linux) { // Linux Desktop
echo 'You are using Linux
';
}

// ---- Test if Firefox ----
if ($firefox) {
echo 'You are using Firefox!
';

// Test Versions
if ($firefox_2) { // Firefox 2
echo 'Version 2';
}
elseif ($firefox_3) { // Firefox 3
echo 'Version 3';
}
elseif ($firefox_3_6) { // Firefox 3.6
echo 'Version 3.6';
}
else { // A version not listed
echo 'What Version do you use?';
}
}

// ---- Test if Safari or Chrome ----
elseif ( ($safari || $chrome) && !$iphone) {
echo 'You are using a webkit powered browser (Safari or Chrome?)
';

if ($safari && !$chrome) { // Test if Safari and not Chrome
echo 'You are using Safari!
';

// Test if Safari Mac or Safari Windows
if ($mac && $safari) { // Safari Mac
echo 'You are using Safari on a Mac
';
}
if ($win && $safari) { // Safari Windows
echo 'You are using Safari on Windows
';
}

// Test Versions
if ($safari_2) { // Safari 2
echo 'Version 2
';
}
elseif ($safari_3) { // Safari 3
echo 'Version 3
';
}
elseif ($safari_4) { // Safari 4
echo 'Version 4
';
}
else {
echo 'What version are you using?';
}
}

elseif ($chrome) { // Test if Chrome
echo 'You are using Chrome!';
}
}

// ---- Test if iPhone with Safari 3.1 ----
elseif ($iphone && $safari_3_1) {
echo 'You are using Safari 3.1';
}

// ---- Test if Internet Explorer ----
elseif ($msie) {
echo 'You are using Internet Explorer!
';

// Test Versions
if ($msie_7) { // Internet Explorer 7
echo 'Version 7';
}
elseif ($msie_8) { // Internet Explorer 8
echo 'Version 8';
}
else {
echo 'What Version do you use?';
}
}

// ---- Test if Opera ----
elseif ($opera) {
echo 'You are using Opera!';
}

// ---- If none of the above ----
else {
echo 'What browser are you using?';
}
}
?>




Edited by shelfimage
Link to comment
Share on other sites

John, going along with my tut on targeting IE, look what I worked out! Is this the best/easiest way to write this? And is there anyway to combine those "if echo" statements in the body tag?

 



{ visibility: inherit; } Target IE Method #6
$msie6        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 6.0') ? true : false;
$msie7        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7.0') ? true : false;
$msie8        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 8.0') ? true : false;
?>
<br />.ie6 p {color:red;}<br />.ie7 p {color:green;}<br />.ie8 p {color:blue;}<br />

>


Some Text




Link to comment
Share on other sites

John, going along with my tut on targeting IE, look what I worked out! Is this the best/easiest way to write this? And is there anyway to combine those "if echo" statements in the body tag?

 



{ visibility: inherit; } Target IE Method #6
$msie6        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 6.0') ? true : false;
$msie7        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7.0') ? true : false;
$msie8        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 8.0') ? true : false;
?>
<br />.ie6 p {color:red;}<br />.ie7 p {color:green;}<br />.ie8 p {color:blue;}<br />

>


Some Text




 

the whole determining what browser, and then the second batch of if statements can all be done using a lot less code. Right now you are writing pretty much the same code all over again. And as I presume you have learnt is that whenever something must be repeated you should use loops to make the code smaller and cleaner.

 

Your code can be rewritten as:

 

for($i = 4; $i    if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE $i.0"))
       echo 'IE '.$i;
}

Link to comment
Share on other sites

well to get the same result as in your example:

 



{ visibility: inherit; } Target IE Method #6
$browser = ''; // declare it to prevent undefined error
for($i = 4; $i    if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE $i.0"))
       $browser = 'ie'.$i;
}
?>
<br />.ie6 p {color:red;}<br />.ie7 p {color:green;}<br />.ie8 p {color:blue;}<br />

>


some text



Edited by krillz
Link to comment
Share on other sites

Cool - looks good! Although it does not seem to be working for IE8. 6 and 7 work as expected though. what do you think?

 

in the for loop just make sure it goes up to 8 as now it stops at 7.

 

by doing $i

 

 

*edit*

 

making it clear in case you did not understand the above.

 

for ($i=4; $i 

Edited by krillz
Link to comment
Share on other sites

As is, it adds a empty class to the body tag for browsers other than IE. Anyway to edit this so it only adds the class to IE browsers?

 

format the string in the forloop so it only adds the class if the browser was recognised

 

{ visibility: inherit; } Target IE Method #6
$browser = ''; // declare it to prevent undefined error
for($i = 4; $i    if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE $i.0"))
       $browser = 'class="ie'.$i.'"';
}
?>
<br />.ie6 p {color:red;}<br />.ie7 p {color:green;}<br />.ie8 p {color:blue;}<br />

>

Link to comment
Share on other sites

I've been doing some reading, and apparently "get_browser" is a more reliable way of doing this. Krillz, or John, do you know how to code the same using get_browser? Here is the manual http://php.net/manual/en/function.get-browser.php and here someone did a tut on it http://www.tuxradar.com/practicalphp/16/5/0. However, unless done in the way Krillz and I did it (class on the body), and only for IE which I want, your no better off than IE CC's. I like the ability to target IE in my main style sheet.

Link to comment
Share on other sites

I've been doing some reading, and apparently "get_browser" is a more reliable way of doing this. Krillz, or John, do you know how to code the same using get_browser? Here is the manual http://php.net/manual/en/function.get-browser.php and here someone did a tut on it http://www.tuxradar.com/practicalphp/16/5/0. However, unless done in the way Krillz and I did it (class on the body), and only for IE which I want, your no better off than IE CC's. I like the ability to target IE in my main style sheet.

 

well if you read about the get_browser function you will se this text:

 

Note: In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.

browscap.ini is not bundled with PHP, but you may find an up-to-date ? php_browscap.ini file here.

While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.

 

So personally I would not use that method as it is not available for php coders right out of the box, so the say. And the solution would only add code, as you would first have to check the return array return['browser'] == 'Internet explorer' and return['version']

 

Also if someone got an old setup of the browscap.ini in worst case scenario they won't be able to find a match with anything newer than IE 6.

 

As I said I would not use a solution like that unless specifically instructed to do so, so if it's a more reliable way, when it comes to IE it got pretty standard headers and are easily matched using preg_match() or strpos() so I see no factor on how using get_browser would be a more reliable way, as it's reliability hangs on the users themselves making sure the config is correctly setup and up-to-date.

 

Also if you need a bunch more info, more than what browser the user has, let say compatibility with stuff you are using in your code, I'd choose phpSniff over get_browser any day.

Edited by krillz
Link to comment
Share on other sites

OK, good, thanks for your thoughts krillz! When it comes to php you know much better than I, so I'll take your word for it. This morning I did a browser shot of that setup you did and it performs perfect in all browsers exept older versions of Opera. Apparently around version 8 and below it used an ie6 signature. I personally don't care about older versions of Opera, as Opera users upgrade continually.

Link to comment
Share on other sites

yeah I just checked opera 8's headers and they are like:

Mozilla/4.0 (compatible; MSIE 6.0; Symbian OS; Nokia 6600/4.09.1; 6329) Opera 8.00 [en]

And we are checking it for MSIE 6.0 so it's a positive match, a way around this could be to check whether the header does not have Opera in it:

 

if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE $i.0") && !strpos($_SERVER["HTTP_USER_AGENT"], "Opera"))

Link to comment
Share on other sites

@Krillz thanks for adding on. I like the idea of looping but I don't deal with IE6 anymore and usually only need an IE7 rule.

 

@Eric - this is a substitute for the native get_browser function that will keep your files up to date and not be dependent upon your host supporting it http://code.google.com/p/phpbrowscap/

 

 

 

The cool thing about working with the Smarty PHP template system is if you need a quick check for an IE browser, you can do something like : {if false !== $smarty.server.HTTP_USER_AGENT|lower|strpos:'msie'} do something {/if} which basically says if it isn't False that there is a match to MSIE then do something.

 

Here is a huge database for UA strings - http://www.zytrax.com/tech/web/browser_ids.htm and http://www.useragentstring.com/pages/useragentstring.php

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...