WordPress.org

Forums

Browser redirect script (7 posts)

  1. dvjtj
    Member
    Posted 9 years ago #

    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

  2. Ming
    Member
    Posted 9 years ago #

    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.

  3. dvjtj
    Member
    Posted 9 years ago #

    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"

  4. Ming
    Member
    Posted 9 years ago #

    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");
    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");
    exit;
    }

    // Find all browsers with "win" in the user_agent
    elseif (stristr($ua, "win")) {
    header("Location: http://example/winallwarning.html");
    exit;
    }

    ?>

  5. Ming
    Member
    Posted 9 years ago #

    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

  6. reverb09
    Member
    Posted 9 years ago #

    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?

  7. Minna
    Member
    Posted 9 years ago #

    nope, it's the extra parentheses, it should be
    if ( stristr($ua, "mac") && stristr($ua, "msie 5") ) {

Topic Closed

This topic has been closed to new replies.

About this Topic