Getting remote IP with email in PHP
November 7, 2005 9:17 PM   Subscribe

I need to record the IP address of people using my email form. The script I have isn't working.

The remote IP should be captured with this line:
$remip = $_ENV['REMOTE_ADDR'];

And then it is inserted into the message like this:
$message="Name: ".$Name."
Message: ".$Message."
Remote IP: ".$remip."
";

But when I receive message the space after "Remote IP" is blank. What's wrong?
posted by punishinglemur to Computers & Internet (7 answers total)
 
Try $_SERVER["REMOTE_ADDR"]? That is how I've always accessed environment variables in PHP.
posted by xmutex at 9:19 PM on November 7, 2005


yup, $_SERVER["REMOTE_ADDR"].

If your web host has php version < 4.1.0, you'll wanna use $http_server_vars[remote_addr] instead.br>
reference: http://us3.php.net/reserved.variables
posted by masymas at 11:19 PM on November 7, 2005


er, that should read $HTTP_SERVER_VARS["REMOTE_ADDR"]
posted by masymas at 11:21 PM on November 7, 2005


Just take a look at phpinfo() and it will tell you which ones output what.
posted by meta87 at 11:27 PM on November 7, 2005


Hell, you could just use ob_start(), call phpinfo() and then do $phpInfoOutput = ob_get_flush() to get ALL the values from phpinfo and tack them onto the end of your mail form for that matter.

ps, var_dump() is your friend. Best way, hands down, of finding what values you could make use of in global/pre-defined variables in the future.
posted by phearlez at 6:10 AM on November 8, 2005


See how this does you:

if (getenv(HTTP_X_FORWARDED_FOR)) {
$ip = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ip = getenv(REMOTE_ADDR);
}
posted by 4easypayments at 8:12 AM on November 8, 2005


Here's the function I use, stolen from somewhere or another and slightly modified. It seems to be foolproof.

function GetIP()
{
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}


Your code would now become

Remote IP: ".GetIP()."";
posted by evariste at 2:45 PM on November 8, 2005


« Older Some good Spanish to English / English to Spanish...   |   Star Trek or Star Wars: Which one is bigger? Newer »
This thread is closed to new comments.