Title: Browser redirect script
Last modified: August 18, 2016

---

# Browser redirect script

 *  [dvjtj](https://wordpress.org/support/users/dvjtj/)
 * (@dvjtj)
 * [21 years ago](https://wordpress.org/support/topic/browser-redirect-script/)
 * Does anyone have a good PHP browser redirect script (specifically one that checks
   to see if the user is using MAC IE5)?
 * And also, where are do you add the PHP code for such a thing? In the index.php
   file in wp-content? I tried doing this once and it didn’t quite work right.
 * Thanks,
 * Tim

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

 *  [Ming](https://wordpress.org/support/users/ming/)
 * (@ming)
 * [21 years ago](https://wordpress.org/support/topic/browser-redirect-script/#post-200654)
 * You’d want the browser check to come as soon as possible if you’re actually redirecting
   to another URL. If you’re using browser detection to change the CSS for instance,
   then you’d need to include it in the header.php file of your current active theme(
   for version 1.5).
 * There are lots of browser redirect scripts on the web and they all vary in the
   number of browsers/versions/operating systems they detect. Search google for ‘
   php browser redirect script’ and you’ll find them and they usually include instructions
   for use.
 * With some more details about why you’re doing the browser detection I can be 
   more specific.
 *  Thread Starter [dvjtj](https://wordpress.org/support/users/dvjtj/)
 * (@dvjtj)
 * [21 years ago](https://wordpress.org/support/topic/browser-redirect-script/#post-200699)
 * I’m doing the browser re-direct because there are some bugs with the website 
   in Internet Explorer on Mac that, rather than fix, would rather just have a redirect
   to a page saying “Please use another browser on this site for best results”
 *  [Ming](https://wordpress.org/support/users/ming/)
 * (@ming)
 * [21 years ago](https://wordpress.org/support/topic/browser-redirect-script/#post-200751)
 * Each browser reports a user agent string to the server. They look something like
   this:
    Mozilla/4.0 (compatible; MSIE 5.15; Mac_PowerPC) (_Not all browsers report
   them and they can also be faked_)
 * Browser detection is nothing more than searching this string for potential matches.
   So we’ll use PHP to check the user agent for “mac” and “msie 5”.
 * `
    <?php // Shorten the string to "$ua" to save my lazy fingers $ua = $_SERVER["
   HTTP_USER_AGENT"];
 * // If "mac" and "msie 5" are found redirect to a different URL
    if (stristr($
   ua, "mac")) && (stristr($ua, "msie 5")) { header("Location: [http://example/macwarning.html](http://example/macwarning.html)");
   exit; }
 * // If no matches are found then exit the script and continue the page
 * ?>
 * You don’t have to redirect away. You could do anything where the Location code
   is including using a different stylesheet or simply printing a message saying“
   Because you’re using Mac IE5 this page may not look correct. Please consider 
   upgrading to …”. This way they’d get the message and at least a peak at the content.
 * I haven’t tested the script but it looks correct. You can add ‘elseif’ statements
   to continue checking for other browsers.
 * `
    <?php $ua = $_SERVER["HTTP_USER_AGENT"];
 * // Find browsers containing "mac" and "msie 5"
    if (stristr($ua, "mac")) && (
   stristr($ua, "msie 5")) { header("Location: [http://example/maciewarning.html](http://example/maciewarning.html)");
   exit; }
 * // Find all browsers with "win" in the user_agent
    elseif (stristr($ua, "win")){
   header("Location: [http://example/winallwarning.html](http://example/winallwarning.html)");
   exit; }
 * ?>
 *  [Ming](https://wordpress.org/support/users/ming/)
 * (@ming)
 * [21 years ago](https://wordpress.org/support/topic/browser-redirect-script/#post-200754)
 * Oh, I know you mentioned you didn’t want to fix the bug but in case you didn’t
   know there are css hacks that specifically target the Mac IE browser allowing
   you to add or exclude styles at will.
 * [http://www.sam-i-am.com/work/sandbox/css/mac_ie5_hack.html](http://www.sam-i-am.com/work/sandbox/css/mac_ie5_hack.html)
 *  [reverb09](https://wordpress.org/support/users/reverb09/)
 * (@reverb09)
 * [20 years, 11 months ago](https://wordpress.org/support/topic/browser-redirect-script/#post-201051)
 * i am trying this on a site i’m building and i get this error:
 * Parse error: parse error, unexpected T_BOOLEAN_AND in /home/massive/public_html/
   dev1/index.php on line 30
 * this is line 30:
 * `if (stristr($ua, "mac")) && (stristr($ua, "msie 5")) {`
 * is it the two ampersands causing the problem?
 *  [Minna](https://wordpress.org/support/users/minna/)
 * (@minna)
 * [20 years, 11 months ago](https://wordpress.org/support/topic/browser-redirect-script/#post-201052)
 * nope, it’s the extra parentheses, it should be
    `if ( stristr($ua, "mac") && 
   stristr($ua, "msie 5") ) {`

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

The topic ‘Browser redirect script’ is closed to new replies.

## Tags

 * [browser](https://wordpress.org/support/topic-tag/browser/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [redirect](https://wordpress.org/support/topic-tag/redirect/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 4 participants
 * Last reply from: [Minna](https://wordpress.org/support/users/minna/)
 * Last activity: [20 years, 11 months ago](https://wordpress.org/support/topic/browser-redirect-script/#post-201052)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
