Title: PHP conditional statement for User Agent / browser?
Last modified: August 19, 2016

---

# PHP conditional statement for User Agent / browser?

 *  [Mike](https://wordpress.org/support/users/maddogmike/)
 * (@maddogmike)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/)
 * I want to present some text only if a visitor’s user agent is Internet Explorer.
 * I found [this plugin](http://wordpress.org/extend/plugins/php-browser-detection/),
   but I was wondering if I can do it without a plugin by using a PHP conditional
   statement.
 * thanks,
    Mike

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [Tim Moore](https://wordpress.org/support/users/tmoorewp/)
 * (@tmoorewp)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1766878)
 * You should be able to use the same PHP conditionals as that plugin does.
 *  Thread Starter [Mike](https://wordpress.org/support/users/maddogmike/)
 * (@maddogmike)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1766924)
 * OK, I tried this:
    `<?php if ( is_IE ) <p>words here</p>; ?>` …but it didn’t 
   work, generated an error
 *  [Tim Moore](https://wordpress.org/support/users/tmoorewp/)
 * (@tmoorewp)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1766969)
 * Mike,
 * This isn’t working because is_IE() is a function defined by the plugin you linked
   to. In order to get it to work correctly, you’ll need to look closely at the 
   code that plugin uses for the function is_IE().
 *  [Pranav Rastogi](https://wordpress.org/support/users/ulgaming/)
 * (@ulgaming)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1766971)
 * You can do it with this:
 *     ```
       <?php
       function detect_IE()
       {
       if(isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') !== false))
       {
       //blablabla
       }
       }
       ?>
       ```
   
 *  Thread Starter [Mike](https://wordpress.org/support/users/maddogmike/)
 * (@maddogmike)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1766989)
 * I tried to just use the plugin, but I can’t activate it because my theme already
   has an “is_IE” function defined. I tried changing the plugin function to “is_MSIE”,
   and using that in my conditional statement, but that didn’t work either.
 * Jeez, I would have thought this would be pretty simple, but nothing can be simple,
   eh? (At least not for me!) 😉
 *  Thread Starter [Mike](https://wordpress.org/support/users/maddogmike/)
 * (@maddogmike)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1766990)
 * Oh Pranav, just saw your post…tried that too and got an error. I’m just trying
   to put this in a php enabled widget to show certain text if the browser is IE.
   What I’m I missing? (Can you tell I’m a PHP-newbie?)
 * thanks,
    Mike
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1767007)
 * WordPress does browser detection already, just check against the declared var..
 *     ```
       <?php if( $is_IE ) : ?>
   
       <!-- your html replaces this line -->
   
       <?php endif; ?>
       ```
   
 *  Thread Starter [Mike](https://wordpress.org/support/users/maddogmike/)
 * (@maddogmike)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1767013)
 * Thanks Mark, I thought WP did browser detection w/o need for a plugin. However,
   I tried your code but couldn’t get it to work. I read somewhere that it should
   be `$is_winIE`, but that didn’t work either. I must be missing something.
 *  [Pranav Rastogi](https://wordpress.org/support/users/ulgaming/)
 * (@ulgaming)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1767018)
 * Mike, what error are you getting from my code? Can you paste it here?
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1767020)
 * You may need to globalise the variable first…
 *     ```
       global $is_IE;
       ```
   
 * You can see the variable declared on lines 63[1] and 69[2].
 * Current
    [1] – [https://core.trac.wordpress.org/browser/tags/3.0.1/wp-includes/vars.php](https://core.trac.wordpress.org/browser/tags/3.0.1/wp-includes/vars.php)
   Trunk [2] – [https://core.trac.wordpress.org/browser/trunk/wp-includes/vars.php](https://core.trac.wordpress.org/browser/trunk/wp-includes/vars.php)
 *  Thread Starter [Mike](https://wordpress.org/support/users/maddogmike/)
 * (@maddogmike)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1767052)
 * Thanks for all the help everybody. Turns out I won’t need this after all.
 * Here’s the backstory, if you’re wondering WTF… After a theme “upgrade”, my site
   started rendering strangely on IE. So I was hoping to quickly implement a message
   to IE users to “pardon our mess” while I fixed the issue. Turns out that implementing
   this conditional statement was more difficult (at least for me) than fixing the
   theme to work in IE. I was able to accomplish the latter before the former, so
   I no longer need the former.
 * So…thanks anyway!
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1767053)
 * Well glad to hear you resolved your problem in any case, the issue with the var
   was probably just a case of code placement as i’ve never had any problem using
   them for browser detection..
 * 😉

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘PHP conditional statement for User Agent / browser?’ is closed to new
replies.

## Tags

 * [browser](https://wordpress.org/support/topic-tag/browser/)
 * [conditional statement](https://wordpress.org/support/topic-tag/conditional-statement/)
 * [user-agent](https://wordpress.org/support/topic-tag/user-agent/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 12 replies
 * 4 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/php-conditional-statement-for-user-agent-browser/#post-1767053)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
