note 76737 added to reserved.variables
| From: | mathiasrav at gmail dot com | Date: | Sat, 28 Jul 2007 20:31:17 +0000 |
| Subject: | note 76737 added to reserved.variables | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129005@lists.php.net to get a copy of this message | ||
Based on dlyaza's snippet posted at 22-Oct-2006 12:33, here's getip() which gets the IP,
first checking some known custom proxy headers for validity and then falling back to REMOTE_ADDR if
none are found/match.
<?php
function getip() {
static $ip = false;
if ($ip !== false) return $ip;
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
'REMOTE_ADDR') as $aah) {
if (!isset($_SERVER[$aah])) continue;
$curip = $_SERVER[$aah];
$curip = explode('.', $curip);
if (count($curip) !== 4) break; // If they've sent at least one invalid IP, break out
foreach ($curip as &$sup) if (($sup = intval($sup)) < 0 or $sup > 255) break 2;
$curip_bin = $curip[0] << 24 | $curip[1] << 16 | $curip[2] << 8 | $curip[3];
foreach (array(
// hexadecimal ip ip mask
array(0x7F000001, 0xFFFF0000), // 127.0.*.*
array(0x0A000000, 0xFFFF0000), // 10.0.*.*
array(0xC0A80000, 0xFFFF0000), // 192.168.*.*
) as $ipmask) {
if (($curip_bin & $ipmask[1]) === ($ipmask[0] & $ipmask[1])) break 2;
}
return $ip = $curip;
}
return $ip = $_SERVER['REMOTE_ADDR'];
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 87.58.61.162
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.php
Edit -- https://master.php.net/note/edit/76737
Del: integrated -- https://master.php.net/note/delete/76737/integrated
Del: useless -- https://master.php.net/note/delete/76737/useless
Del: bad code -- https://master.php.net/note/delete/76737/bad+code
Del: spam -- https://master.php.net/note/delete/76737/spam
Del: non-english -- https://master.php.net/note/delete/76737/non-english
Del: in docs -- https://master.php.net/note/delete/76737/in+docs
Del: other reasons-- https://master.php.net/note/delete/76737
Reject -- https://master.php.net/note/reject/76737
Search -- https://master.php.net/manage/user-notes.php