treehouse : what would you like to learn today?
Web Design Web Development iOS Development

PHP redirect by IP Address [help with Code]

  • Hi people,

    I am trying to redirect people on my site depending on their IP Address, and more specifically everyone who's IP doesn't match the one specified in the array, in my code.

    Here is my code:

    <?php
    $allowed = array("94.171.121.121");
    $ip = $REMOTE_ADDR;
    if(!in_array($ip, $allowed))
    {
    header("Location: http://www.google.co.uk); /* Redirect browser */
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    }
    else
    {

    }
    ?>


    The problem is it redirects everyone even the specified IP address.
    Any tips how to make it work?

    Regards,
    Aleksander
  • Try using $_SERVER['REMOTE_ADDR'] instead of $REMOTE_ADDR:
    $ip = $_SERVER['REMOTE_ADDR'];