#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    39
    Rep Power
    0

    Simple IP address check problem


    Ok, I am working on a page, I put my IP address in a string, then I have the code check the IP address of the visitor to see if the IP address is the same as mine that I hard coded so I can test things.

    The problem is, that it fails every time. I am not sure it's because of the periods in the ip address or what... here is the code:

    PHP Code:
    if($_ENV['REMOTE_ADDR'] == "$_compIp") {
      echo 
    "True";
    } else {
      echo 
    "False";

    that prints False, but when I have it print the both the IP addresses they are the same in the string:

    PHP Code:
    echo "'" $_ENV['REMOTE_ADDR'] . "'<br>";
    echo 
    "'" $_compIp "'"
    that outputs: the same ip address in both quote marks, so it should be the same so the == should be TRUE.

    I also tried this:
    PHP Code:

    $cmp 
    strcmp($_ENV['REMOTE_ADDR'], $_compIp);
    if(
    $cmp 1) {
     echo 
    "Compare passed";
    } else {
    echo 
    "Compare failed";

    that prints the failed message every time. So even the compare is not working.

    since
    PHP Code:
    $_ENV['REMOTE_ADDR'
    contains the ip address and it matches the one I hard coded to
    PHP Code:
    $_compIp 
    I don't know why it is not working.

    any idea how to make this work? I only need it while I am testing...

    Thank you for your help if you can provide it.
    Rich
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    PHP Code:
    echo $_SERVER['REMOTE_ADDR']; 
    that's your ip (see the manual )


    p.s notice the difference when testing it locally and on a live server
    Last edited by aeternus; February 20th, 2013 at 06:30 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    39
    Rep Power
    0
    Originally Posted by aeternus
    PHP Code:
    echo $_SERVER['REMOTE_ADDR']; 
    that's your ip (see the manual )


    p.s notice the difference when testing it locally and on a live server
    thank you for your reply, I changed it and it still did not work:

    PHP Code:
    if($_SERVER['REMOTE_ADDR'] == "$_compIp") {
    echo 
    "True!!";
    } else {
    echo 
    "Still False!";

    output: Still False!

    even though the IP address I echo above it in the Server remote addr is the same one In the string I am comparing.

    Maybe I should explode the ip addresses into two arrays and check each sector against each other... individually, that is overkill, but it may work since this is not.

    I am testing these on a live server, Unix Server running latest Apache and Php 5.x

    any idea why it still is not working?

    Thx again,
    Rich
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Remove the quotes around $_compIp ?
  8. #5
  9. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    as (implicitly) suggested you first echo out $_SERVER['REMOTE_ADDR']
    Than you know exactly what value it has. Compare that to your hardcoded value.
    Echo them both so you know exactly what is going on
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    39
    Rep Power
    0
    Originally Posted by Aurum84
    Remove the quotes around $_compIp ?
    done.... still false

    crazy

    Rich
  12. #7
  13. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    notice my post...
    Originally Posted by aeternus
    as (implicitly) suggested you first echo out $_SERVER['REMOTE_ADDR']
    Than you know exactly what value it has. Compare that to your hardcoded value.
    Echo them both so you know exactly what is going on
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    39
    Rep Power
    0
    Originally Posted by aeternus
    as (implicitly) suggested you first echo out $_SERVER['REMOTE_ADDR']
    Than you know exactly what value it has. Compare that to your hardcoded value.
    Echo them both so you know exactly what is going on
    I did that... that is where I got my Ip address to put in the string.

    I even echo both in double " so I can copy them exact to see if there is a empty space in the hard coded ip, but there is not.

    Ok, I went back and re-copied the value, pasted it in the string and instead of putting:
    PHP Code:
    $_compIp '11.12.13.14'
    I put:
    PHP Code:
    $_compIp "11.12.13.14"
    then that worked. I am not sure if it was me re-copying the IP address or putting double quotes around the Stored IP address in the string...

    at any rate it is working now.

    Thank you very much for your assistance. I appreciate it.

    Rich
  16. #9
  17. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    good to hear, always just echo out all values you are testing with. saves you a headache

    P.s. you might want to read a bit about what the difference is between single and double quotes.
    Last edited by aeternus; February 20th, 2013 at 07:05 PM.

IMN logo majestic logo threadwatch logo seochat tools logo