PHP rookie question

Status
Not open for further replies.

DaveEMG

Pwner.
Jan 11, 2007
889
7
0
Silicon Valley, CA
www.evokemg.com
Alright, I can't find this... What's the server variable in PHP for the user's IP address? I just need to do an email signup form for a page, and I want to cap IP for CANSPAM compliance...

Sorry, total PHP rook here. I could do this in ASP in about 2 seconds, but can't find the magic in linux.

All help is appreciated...
 


To get the "real" IP, (not a proxy, etc.. use this)

Code:
$ip = 0;

if (isset($_SERVER['HTTP_CLIENT_IP']) && ip2long($_SERVER['HTTP_CLIENT_IP']) > 0) {
	$ip = $_SERVER['HTTP_CLIENT_IP'];
}		

if (!$ip && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
	foreach (explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']) as $arr_ip) {
		if (ip2long($arr_ip) > 0) {
			$ip = $arr_ip;
		}
	}
}

if (!$ip && isset($_SERVER['HTTP_X_FORWARDED_FOR']) && ip2long($_SERVER['HTTP_X_FORWARDED']) > 0) {
	$ip = $_SERVER['HTTP_X_FORWARDED'];
} else if (!$ip && isset($_SERVER['HTTP_FORWARDED_FOR']) && ip2long($_SERVER['HTTP_FORWARDED_FOR']) > 0) {
	$ip = $_SERVER['HTTP_FORWARDED_FOR'];
} else if (!$ip && isset($_SERVER['HTTP_FORWARDED']) && ip2long($_SERVER['HTTP_FORWARDED']) > 0) {
	$ip = $_SERVER['HTTP_FORWARDED'];
} else if (!$ip) {
	$ip = $_SERVER['REMOTE_ADDR'];
}
 
one thing:

ip2long is signed - use
Code:
$longip = sprintf( "%u", ip2long( $ip ) );




btw. same with crc32 (if you have to use it someday):
Code:
$hash = sprintf( "%u", crc32( $string ) );
 
Status
Not open for further replies.