php browser detection

Browser detection with php

Submitted by GHS, Feb 2007.
This tutorial explains how to make a code only works in one separated browser, I think that’s useful to correct incompatibility of page style in some browsers.



In your page, put:

Code:

  1.  
  2. $user_agent = isset($_SERVER[‘HTTP_USER_AGENT’]) ? $_SERVER[‘HTTP_USER_AGENT’] : ”;
     
  3. if(strpos($user_agent, ‘Opera’) !== false)
     
  4. {
     
  5.    echo ‘code for Opera’;
     
  6. }
     
  7. elseif(strpos($user_agent, ‘Gecko’) !== false)
     
  8. {
     
  9.    echo ‘code for Mozilla/Firefox’;
     
  10. }
     
  11. elseif(strpos($user_agent, ‘MSIE’) !== false)
     
  12. {
     
  13.    echo ‘code for IE’;
     
  14. }
     
  15. else
     
  16. {
     
  17.    echo ‘some other browser’;
     
  18. }
     

  19.  
  20. ?> 
Done, now modify the code to works how you want. This tutorial only explains how to show different codes for Internet Explorer, Mozilla browsers, Opera and show anything to all the other browsers.

Share this tutorial

If you want to show this tutorial to someone else please use following code to link to this tutorial:

HTML Link:
Browser detection with php” class=”text” onclick=”this.focus(); this.select();”/>
Forum BBCode:

Content of this tutorial may not be published anywhere else without author’s permission. Please link to this page instead.