by Udt
8 replies
Hey guys,

I was given this code by a fellow member but I didn't want to bother him again so I was wondering if anybody else knew.

<?php

$ip_address = $_SERVER['REMOTE_ADDR'];

if ($ip_address == '108.7.3.52')
header("Location: http://myurl.com/http://page.html");
else
header("Location: http://myurl.com/page2.html");

?>

I was wondering if anybody knows the function for an array (more than 1 ip).

Thank you in advance!

D
#php #redirect
  • Profile picture of the author lisag
    $ipaddresses = array('108.7.3.52','108.7.3.53','108.7.3.54');

    if (in_array($_SERVER['REMOTE_ADDR'],$ipaddresses]))
    {
    header("Location: http://myurl.com/http://page.html");
    } else {
    header("Location: http://myurl.com/page2.html");
    }
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1634473].message }}
    • Profile picture of the author Udt
      Thank you lisa!!
      Signature
      http://Mukdo.com - All the deals from slickdeals,fatwallet, and bensbargains in one place!
      {{ DiscussionBoard.errors[1635727].message }}
      • Profile picture of the author lisag
        Originally Posted by Udt View Post

        Thank you lisa!!
        You're welcome.

        Be aware that this line, as you presented it, won't work:

        header("Location: http://myurl.com/http://page.html");

        Because of the double http.

        Should be something like: header("Location: http://myurl.com/page.html");
        Signature

        -- Lisa G

        {{ DiscussionBoard.errors[1635751].message }}
        • Profile picture of the author Udt
          Thanks again =)
          Signature
          http://Mukdo.com - All the deals from slickdeals,fatwallet, and bensbargains in one place!
          {{ DiscussionBoard.errors[1636077].message }}
          • Profile picture of the author Udt
            Hey Lisa,

            For some reason, the code isn't working ( I think it is because of those brackets? } )

            The code I have now is:

            <?php

            $ipaddresses = array('108.7.3.52','108.7.3.53','108.7.3.54');

            if (in_array($_SERVER['REMOTE_ADDR'],$ipaddresses]))
            {
            header("Location: http://myurl.com/http://page.html");
            } else {
            header("Location: http://myurl.com/page2.html");
            }

            ?>

            Is there something wrong I wrote?
            Signature
            http://Mukdo.com - All the deals from slickdeals,fatwallet, and bensbargains in one place!
            {{ DiscussionBoard.errors[1636089].message }}
            • Profile picture of the author BrianLeanza
              Hey,

              you have to make sure the header() function is called before any output is sent.
              For instance:
              Code:
              <html>
               <?php 
                  header("Location: http://www.google.com");
              will NOT work. Because of the <html> which is considered as output.

              Also you might have to fix your URLs
              instead of
              Code:
              header("Location: http://myurl.com/http://page.html");
              use
              Code:
              header("Location: http://myurl.com/page.html");
              Hopefully this was of any help,

              Cheers!
              {{ DiscussionBoard.errors[1637583].message }}
  • Profile picture of the author iampick
    To make sure that the code will work, Put it at the first line.
    {{ DiscussionBoard.errors[1638455].message }}

Trending Topics