Home » Web development » Cross Browser Compatibility » How to write Browser Specific HTML CSS in PHP

Here is code which Web designer and web developer can use to make some code browser specific only in php. Many times Web designer and web developer require to put in browser-specific HTML / CSS in your files, because most often the pages are rendered differently (esp. IE). You can detect what browser the user is using with the help of PHP.

Demo: Browser Specific HTML CSS in PHP

In your PHP file, put the following code first:

<?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; ?>

And use where you want to execute code based on browser

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

// Safari or Chrome. Both use the same engine - webkit
if ($safari || $chrome) {
echo 'you are using a webkit powered browser';
echo '
';
}

// IE
if ($msie) {
echo '
you are using Internet Explorer
';
echo '
';
}

// Not IE and for all other browsers
if (!$msie) {
echo '
you are not using Internet Explorer
';
echo '
';
}

?>

If you are looking for IE-only conditional statements, refer http://www.quirksmode.org/css/condcom.html

Other useful resources:

http://php.net/manual/en/reserved.variables.server.php
http://php.net/manual/en/function.get-browser.php
http://php.net/manual/es/function.get-browser.php
http://w3resource.com/php/super-variables/$_SERVER.php

Array ( [0] => Cross Browser Compatibility, Web development )