Sunday, March 29, 2015

How to detectet the Browser (i.e. IE, Firefox, Safari, Chrome) ?

Browser detection - IE, Firefox, Safari,
Chrome Many times you 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. In your PHP file, put the following code first:
  1. <?php
  2. $msie = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false;
  3. $firefox = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
  4. $safari = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
  5. $chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
  6. ?>
And to add, conditional HTML or CSS, put it like this:
  1. <?php
  2. //Firefox
  3. if ($firefox) {
  4. echo 'you are using Firefox!';
  5. echo '<br />';
  6. }
  7.  
  8. // Safari or Chrome. Both use the same engine - webkit
  9. if ($safari || $chrome) {
  10. echo 'you are using a webkit powered browser';
  11. echo '<br />';
  12. }
  13.  
  14. // IE
  15. if ($msie) {
  16. echo '<br>you are using Internet Explorer<br>';
  17. echo '<br />';
  18. }
  19.  
  20. // Not IE and for all other browsers
  21. if (!$msie) {
  22. echo '<br>you are not using Internet Explorer<br>';
  23. echo '<br />';
  24. }
  25.  
  26. // Add inline css
  27. if ($firefox) {
  28. echo '<style type="text/css">';
  29. echo '.mydiv {position:relative; width:100px; height:50px;}';
  30. echo '</style>';
  31. }
  32. ?>

No comments:

Post a Comment