IP's don't match

Discussion in 'PHP' started by stephan2307, Aug 22, 2011.

  1. #1
    Hi,

    if I use the following script and then call the script via file_get_contents() the IP's don't match even though the request comes from that domain.

    
    $userIP = $_SERVER['REMOTE_ADDR'];
    $serverIP = gethostbyname($result['domain']);
    echo $userIP;echo '|';
    echo $serverIP;
    if ($userIP != $serverIP) {
        echo 'IP DID NOT MATCH';
        exit;
    }
    PHP:
    Any idea why this is?
     
    stephan2307, Aug 22, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    Sorry, I'm confused, a users IP has nothing to do with a hosted sites IP, so I don't see why they would match ?

    Maybe I'm wrong understanding your query ?
     
    MyVodaFone, Aug 22, 2011 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    ok here is a bit more info.

    I am working on an api. Users will get a key which is linked to a domain. I will run a check to see if the IP of the request matches the domain name the key belongs to.
     
    stephan2307, Aug 22, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Just remember, users IP's will change from time to time... you might what to look at

    $userIP = gethostbyaddr($_SERVER['REMOTE_ADDR']);
    PHP:
    ..and strip out the IP because it will change and then check against the ISP name. The only problem with that is, people are using work computers or are mobile with laptops so both the IP's and ISP's will change pending on locations.
     
    MyVodaFone, Aug 22, 2011 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    No its not the users IP it is a servers IP that I am matching
     
    stephan2307, Aug 22, 2011 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    ? $_SERVER['REMOTE_ADDR']; is the users IP address

    $_SERVER['SERVER_ADDR']; is the IP on which the script is running on

    to get an IP of a hosted site

    
    <?php
    
    $userIP = $_SERVER['SERVER_ADDR'];
    $serverIP = gethostbyname('example.com'); // put your domain name in here
    echo $userIP;echo '|';
    echo $serverIP;
    if ($userIP != $serverIP) {
        echo 'IP DID NOT MATCH';
        exit;
    }
    
    ?>
    
    PHP:
     
    MyVodaFone, Aug 22, 2011 IP
  7. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #7
    ok so let me explain again.

    I am going to offer some API's

    People will be able to use it for free but they will need a key to use them

    Each key will be unique for a domain.

    So if someone wants to use the API they register their domain and get the key. Then they can start to use it. When a request comes in, I want to make sure that the request comes from the server where the domain is located on.

    Example

    example.com is located on 12.12.12.12
    when a request comes in I take the key and get the corresponding domain. From that domain I get the IP which would be 12.12.12.12 in this example. I want to compare this IP with the IP of the request which should be identical.

    In my code however the IP's are slightly different so in this example might give 12.12.12.8
     
    stephan2307, Aug 22, 2011 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    
    
    SELECT domain FROM domains WHERE domain_key='$key';
    
    
    Code (markup):
    Anyway I'll leave it there... good luck with your apps
     
    MyVodaFone, Aug 22, 2011 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    The request for a key is going to come from the user. The key is going to be used on the user's server. They IP addresses are almost never the same. (They'll be the same if the user is hosting his own server, which is almost never the case.)

    You'd have to have the user request the key using software that's running on his site in order to get the same IP addresses.

    All you can do is connect the key to the requested server address (in your database) and only serve data for that key if the remote address - at the time the service is being used - is the one in the database.
     
    Rukbat, Aug 22, 2011 IP