IP Address: Capture and Format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

IP Address: Capture and Format

Post by tmaiden »

I was wondering if anyone could tell me how to capture a person's ip address who visits my site.

I've seen $_SERVER['REMOTE_ADDR'] and was wondering if this was the best way of doing so.

Also, lets say the user's ip address is: 203.279.16.123 OR 203.279.16.55
How can I have it displayed as 203.279.16.xxx OR 203.279.16.xx

Thanks.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

If the user comes through a non-anonymising proxy, that field will have the ip of the proxy. The real ip will be supplied in a http header ($_SERVER['HTTP_something']), for example HTTP_VIA, HTTP_X_FORWARDED_FOR, HTTP_FORWARDED_FOR etc.

About display - explode() the ip dotted quad into an array, change the last item to 'xx', and then join() it again
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

We've had many many threads involving IP addresses, specifically REMOTE_ADDR.. take a look at them.

Quick and cheap for changing the display is explode() as Mordred said.
Post Reply