Jump to content
Stef's Coding Community

FireFox Google Chrome Browser Detecion using conditional comments hack


Recommended Posts

I have a need to do a few things first i wanted to be able to send php code to multiple browsers but not the same code. I ran into a problem with this and on top of that, i wanted to send multiple stlyesheets to different browsers. CC's worked for the second task but I had to make a few "new" hacks to get them to serve my purposes with PHP. These "hacks" that i have created are designed to target 3 browsers, and differientiate between the 3. 2 of which are not IE, meaning firefox, and Google Chrome; using these following "hacks" in conjunction with a "" allows one to distinguish and pass different (x)HTML, CSS and PHP data to the browsers.

 

I have these set up in a seperate php file than the actual "index" file of the webpage and they are included by employing a SSI. Below is a copy of that file.

 

Detect IE, firefox and chrome hacks by : p4c - nick gibson

 

i hope these will be some help and service to others. they are for me.:)

 

-------------------------------------------------------------here is the file----------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

________________________________________end of file__________________________________________________

 

 

it is my hope that these will perhaps suppliment the * html hack that no longer works, due to IE7.

 

another thing is that chrome doesnt like background-image styles from imported sheets, but if you append

 

the holy * html hack like so the issue goes away.

 

/* Hides from IE5-mac \*/

* html body{height: 1%;} /* ENABLES CHROME TO BE CSS2 COMPLIANT */

/* End hide from IE5-mac */

 

good day, and god bless -- keep coding keyboard samuris

Link to post
Share on other sites
  • 1 month later...

If using PHP anyway, why not use PHP to detect the browser and send the content desired?

 

For example, you could check what User Agent is being used and then test for each one

 

<?php 

$msie = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false; 
$firefox = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
$safari = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
$chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;

if ($msie == true) {
echo 'MSIE';
}
if ($firefox == 'true') {
echo 'Firefox';
}

?>

Link to post
Share on other sites




<?php
if ($firefox) { //Firefox?
echo 'you are using Firefox!';
}

if ($safari || $chrome) { // Safari?
echo 'you are using a webkit powered browser';
}

if (!$msie) { // Not IE?
echo '
you are not using Internet Explorer
';
}
if ($msie) { // IE?
echo '
you are using Internet Explorer
';
}
?>




Link to post
Share on other sites

Thanks for explaining John! :) That is a very simple way of doing it. It's easier than the green beast http://green-beast.com/experiments/support/P_php_browser_sniffer.phps Any way to target the versions of IE with the PHP (rather than with the CC's) like Green beast did?

 

You may find this interesting. All the ways to target IE only http://www.visibilityinherit.com/code/target-ie.php Now I can add this one in there...

Edited by Eric
Link to post
Share on other sites

I know about the jQuery browser detection classes, but I don't use it b/c it depends upon the browser (not server side).

I use the Green Beast method b/c it works well. But the function it uses will be out of PHP soon (http://php.net/manual/en/function.eregi.php)

 

I always used CC's with Mike's method anyway b/c some PC's have a screwed up Vector Version and might match as IE6 although it is really IE7. So, I used the CC's as an extra careful step.

 

----

With the method I worked out above, you should be able to target IE versions by doing something like this (IE 7):

 

   if ($msie && strpos($_SERVER["HTTP_USER_AGENT"], '7.0') ? true : false ) {
   echo 'This is IE7';
   }

 

But, if you are going to go through the trouble of doing that, we may as well rewrite our variables to match the IE versions immediately.

 

$msie7        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7.0') ? true : false;
$msie8        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 8.0') ? true : false;

 

and then you can use it to send CSS by

 

    if ($msie7) {
   echo 'your ie7 CSS';
   }

   if ($msie8) {
   echo 'your IE 8 CSS';
   }

 

 

If you are going to test for browser versions, you need to check their UA string. Firefox looks something like this

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6

and IE looks like

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0

Link to post
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...
×
×
  • Create New...