Yet another simple question on IP's

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
joetheeskimo5
Forum Commoner
Posts: 43
Joined: Sun Dec 14, 2003 4:47 pm
Location: US
Contact:

Yet another simple question on IP's

Post by joetheeskimo5 »

How can I get the user's IP address? Is there a function for it? I want to use it for things like banning offenders from my site, displaying certain content to certain users (like admins or thread-starters for example), or redirecting a user to a certain page depending on their IP.

Thanks if you can help.

Joe
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Code: Select all

<?php

$usersIP = $_SERVER["REMOTE_ADDR"];

?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

$_SERVER['REMOTE_ADDR']

RTM
joetheeskimo5
Forum Commoner
Posts: 43
Joined: Sun Dec 14, 2003 4:47 pm
Location: US
Contact:

Post by joetheeskimo5 »

Holy crap, you guys replied fast!

Thanks btw.

Joe :)
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

displaying certain content to certain users (like admins or thread-starters for example)
This is not recommended using just their IP. IP's are dynamic and I've heard that AOL users may even have a different IP everytime they do a http request. You are better off using sessions or cookies.

Banning people by IP's may work for a little, but it won't stop a determined user.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

BTOpenworld (UK) also change the users IP everytime they connect as well, so as DuFF said don't expect it to be very reliable.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

For getting the IP address of the user, I use this bit of code I stole from somewhere.

Code: Select all

<?php
	// Get user's IP address.
	if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
	else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
	else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
	else $ip = "UNKNOWN";
?>
But I use this in conjunction with sessions, which is why REMOTE_ADDR is third. Because if the user's IP address keeps changing (due to multiple proxy servers, for example), then session verification with IP isn't so great.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

fyi: anyone malicious in a wya they can be a detriment is likely toknow how to use a proxy, therefore ip isn't a big help. add to that unless you ban by domain blocks, they will get a new one each time if they are on dial up. there's also other issues that make ip banning something that should be a lst resort at best.

find someething else to do to ban a user.'

i decided to do so by email address.
yeah, easy enough to sign up with a new one, but after a while they will tire of it. but i also keep certain other information that once they get to a few dozen ignups i can get their isp to drop them, and any subsequent ones as well for breach of terms of service
Post Reply