Forum Moderators: coopster

Message Too Old, No Replies

getenv("REMOTE_ADDR")

Is returning a localhost IP?

         

trillianjedi

2:41 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is strange:-

$IPAddress = getenv("REMOTE_ADDR");

.... works fine when I access the page from home, but if I access the page from my office (behind 2 NAT devices and a firewall) I get:-

127.255.255.255

No, I'm not hosting the pages at my office... ;)

Any known problems with getenv?

$IPAddress = $_SERVER['REMOTE_ADDR'];

..... does the same.

Thanks,

TJ

coopster

2:59 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Try hitting [whatismyip.org...] and what do you get? Same thing? If so, it's likely your firewall there at the office.

trillianjedi

3:06 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That works perfectly Coop.

So has to be a problem my end, or in my code.

Just realised - I am using INET_ATON() to store this in the DB, so I guess that's the more likely culprit.

coopster

3:07 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh! So that value is not being dumped from the $_SERVER variable at that particular exit point, you are actually getting that value after you retrieve it from the database?

trillianjedi

3:09 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes - sorry, my bad. I completely forget I was encoding/decoding it.

Just trying to dump it live at that point to see if it's the encoding of it that's messing it up (bet it is).

Even so, I still don't really understand why the encoding would work from some IP's and not others?

trillianjedi

3:13 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, done that - IP address is being obtained absolutely correctly from the $_SERVER environment variable.

So storing it then goes something like this (specifics removed but you'll get the gist:-

$sql = "INSERT INTO db (ip) VALUES('INET_ATON('".$IPAddress."'))";

And retrieved something like:-

$sql = "SELECT INET_NTOA(`ip`) AS ipadd FROM db";

echo $row['ipadd'];

And it's sometimes working, sometimes not...

Any thoughts?

coopster

3:14 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Maybe this is why?


Note: When storing values generated by INET_ATON() [dev.mysql.com], it is recommended that you use an
INT UNSIGNED
column. If you use a (signed)
INT
column, values corresponding to IP addresses for which the first octet is greater than 127 cannot be stored correctly.

trillianjedi

3:21 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, good call - now that would make complete sense and explain exactly why it's "topping out" at 127.255.255.255.

Problem is, MySQL is telling me it is storing as an unsigned int, so I suspect I'm misreading something here.

This is the information for that field:-

ip int(11) UNSIGNED

I don't know why it's 11 bytes actually - surely should be 4?

coopster

3:41 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



4 bytes, yes, but what you are seeing there is the
PRECISION
.

But creating an INTEGER column (on my version of MySQL at least) will come back with a column defintion of

INT(11)
. Whereas if you create a column defined as
INT UNSIGNED
and DESCRIBE the table you will see the column defined as
INT(10) UNSIGNED
.

Side note:
Both PRECISION and UNSIGNED are MySQL extensions to the standard.
But this has absolutely nothing to do with your issue, mind you ;-)

trillianjedi

4:00 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Coop - many thanks for your help.

I recreated the table structure without any precision entered so MySQL would do it's own thing and set an int field, unsigned. It came back showing as int(10).

It's now working perfectly ;)

Thanks again.

TJ