1

I am trying to create a page that shows the user their zip code when they are on my page.

(if any of you are familiar with GeoIP data, thats what I am using. )

I have a conversion that converts the users IP address into an IP number, that conversion is:

ipnum = 16777216*w + 65536*x + 256*y + z

where w.x.y.z are the ip sections (000.000.000.000)

My question is, using

$_SERVER['REMOTE_ADDR'];

is there a way for me to section the users ip address and assign the section of the ip address to variables?

for example:

usersip = 192.168.123.5

w = 192; x = 168; y = 123; z = 5;

Thanks!

Ryan
  • 433
  • 1
  • 11
  • 29
  • Note that this approach will stop working in [October](http://en.wikipedia.org/wiki/IPv4_address_exhaustion). – mario Feb 03 '11 at 07:28
  • @mario: why? ipv4 is not deprecated. It is just exhausted. – zerkms Feb 03 '11 at 07:30
  • @zerkms: Nevertheless there will be more clients showing up not having a dotted address (CGI uses the same env var for v6 addresses), which could eventually screw the processing logic. Oh well, not uber relevant at the very moment, but good to keep in mind. – mario Feb 03 '11 at 07:32

1 Answers1

8
list($w, $x, $y, $z) = explode('.', $ip);

Also, instead of math you could convert your ip to int with ip2long

zerkms
  • 249,484
  • 69
  • 436
  • 539