The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Simple IP address check problem
Discuss Simple IP address check problem in the PHP Development forum on Dev Shed. Simple IP address check problem PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 20th, 2013, 06:09 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 39
Time spent in forums: 12 h 16 m 18 sec
Reputation 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 contains the ip address and it matches the one I hard coded to 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
|

February 20th, 2013, 06:26 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
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.
|

February 20th, 2013, 06:43 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 39
Time spent in forums: 12 h 16 m 18 sec
Reputation Power: 0
|
|
Quote: | 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
|

February 20th, 2013, 06:48 PM
|
|
|
|
Remove the quotes around $_compIp ?
|

February 20th, 2013, 06:50 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
|
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
|

February 20th, 2013, 06:50 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 39
Time spent in forums: 12 h 16 m 18 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Aurum84 Remove the quotes around $_compIp ? |
done.... still false
 crazy
Rich
|

February 20th, 2013, 06:52 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
notice my post...
Quote: | 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 |
|

February 20th, 2013, 06:59 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 39
Time spent in forums: 12 h 16 m 18 sec
Reputation Power: 0
|
|
Quote: | 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
|

February 20th, 2013, 07:03 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|