note 43194 modified in function.getenv by vincent
| From: | vincent@php.net | Date: | Thu, 17 Jun 2004 07:26:01 +0000 |
| Subject: | note 43194 modified in function.getenv by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71200@lists.php.net to get a copy of this message | ||
I have an application where I want to track the remote address, and if that's not available,
then the local address. (Sometimes it's over a network, and sometimes over the internet, and
for security, some features should only be run over the local network.) I added some checking that
should be redundant to get around a few weird situations I encountered, where addresses were set,
but empty.
<?php
$private_net_ip_masks = array( '10.0.0.', '192.168.', '127.0.0.',
'172.16.0.' );
if( isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
$_SERVER['HTTP_X_FORWARDED_FOR'] != '' )
{
$ipStrings = explode( ',',$_SERVER['HTTP_X_FORWARDED_FOR']);
foreach($ipStrings as $k => $v)
{
if( empty($v) )
{
unset( $ipStrings[$k] );
}
else
{ // set the first one we find as the default. Little dirty, but it works.
if(!isset($ipString)) $ipString = $v;
}
}
}
if( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] !=
'' )
{
$ipStrings[] = $_SERVER['REMOTE_ADDR'];
if(!isset($ipString)) $ipString = $_SERVER['REMOTE_ADDR'];
}
foreach($ipStrings as $k1 => $ip)
{
foreach($private_net_ip_masks as $k2 => $pip)
{
if(strpos($ip, $pip) === 0)
{ // local ip
unset($ipStrings[$k1]);
break;
}
}
}
if( !empty($ipStrings) )
{
foreach( $ipStrings as $v )
{
if(!empty($v))
{
$ipString = $v;
$is_local_ip = false;
break;
}
}
}
else
{
$is_local_ip = true;
}
$ipArray = explode('.', $ipString);
// Spit out the results
foreach($ipStrings as $k => $v) echo '$ipStrings['.$k.'] = ' . $v .
'<br />';
echo "\$ipString = $ipString<br />";
foreach($ipArray as $k => $v) echo '$ipArray['.$k.'] = ' . $v . '<br
/>';
?>
--was--
I have an application where I want to track the remote address, and if that's not available,
then the local address. (Sometimes it's over a network, and sometimes over the internet, and
for security, some features should only be run over the local network.) I added some checking that
should be redundant to get around a few weird situations I encountered, where addresses were set,
but empty.
<?php
$private_net_ip_masks = array( '10.0.0.', '192.168.', '127.0.0.',
'172.16.0.' );
if( isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
$_SERVER['HTTP_X_FORWARDED_FOR'] != '' )
{
$ipStrings = explode( ',',$_SERVER['HTTP_X_FORWARDED_FOR']);
foreach($ipStrings as $k => $v)
{
if( empty($v) )
{
unset( $ipStrings[$k] );
}
else
{ // set the first one we find as the default. Little dirty, but it works.
if(!isset($ipString)) $ipString = $v;
}
}
}
if( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] !=
'' )
{
$ipStrings[] = $_SERVER['REMOTE_ADDR'];
if(!isset($ipString)) $ipString = $_SERVER['REMOTE_ADDR'];
}
foreach($ipStrings as $k1 => $ip)
{
foreach($private_net_ip_masks as $k2 => $pip)
{
if(strpos($ip, $pip) === 0)
{ // local ip
unset($ipStrings[$k]);
break;
}
}
}
if( !empty($ipStrings) )
{
foreach( $ipStrings as $v )
{
if(!empty($v))
{
$ipString = $v;
$is_local_ip = false;
break;
}
}
}
else
{
$is_local_ip = true;
}
$ipArray = explode('.', $ipString);
// Spit out the results
foreach($ipStrings as $k => $v) echo '$ipStrings['.$k.'] = ' . $v .
'<br />';
echo "\$ipString = $ipString<br />";
foreach($ipArray as $k => $v) echo '$ipArray['.$k.'] = ' . $v . '<br
/>';
?>
http://php.net/manual/en/function.getenv.php