REMOTE_ADDR w/ one valid IP (no commas or spaces)'."\n";
echo '
"'.$this_ip.'"
'."\n";
demo_phpBB($this_ip);
demo_MOD($this_ip);
$this_ip = '1.2.3.4,1.2.3.5, 1.2.3.6 1.2.3.7';
echo 'More than one IP, variable seperator
'."\n";
echo '"'.$this_ip.'"
'."\n";
demo_phpBB($this_ip);
demo_MOD($this_ip);
$this_ip = ' 1.2.3.4,1.2.3.5, 1.2.3.6 1.2.3.7 ';
echo 'Same as above + extraneous leading/ending spaces
'."\n";
echo '"'.$this_ip.'"
'."\n";
demo_phpBB($this_ip);
demo_MOD($this_ip);
// phpBB 3.0.7-PL1 default behavior in session_begin()
function demo_phpBB($this_ip)
{
echo 'phpBB:
'."\n";
$s = microtime(true);
$this_ip = preg_replace('#[ ]{2,}#', ' ', str_replace(array(',', ' '), ' ', $this_ip));
// split the list of IPs
$ips = explode(' ', $this_ip);
// Default IP if REMOTE_ADDR is invalid
$this_ip = '127.0.0.1';
foreach ($ips as $ip)
{
// check IPv4 first, the IPv6 is hopefully only going to be used very seldomly
if (!empty($ip) && !preg_match(get_preg_expression('ipv4'), $ip) && !preg_match(get_preg_expression('ipv6'), $ip))
{
// Just break
break;
}
// Use the last in chain
$this_ip = $ip;
}
$e = microtime(true);
$time = round(($e-$s)*1000,6);
echo '"' . $this_ip . '"
'."\n";
echo 'Took: '.$time.' ms
'."\n\n\n";
}
// Modified behavior
function demo_MOD($this_ip)
{
echo 'MOD:
'."\n";
$s = microtime(true);
if (strpos($this_ip, ',') !== FALSE || strpos($this_ip, ' ') !== FALSE)
{
$this_ip = preg_replace('# {2,}#', ' ', str_replace(',', ' ', $this_ip));
// split the list of IPs
$ips = explode(' ', trim($this_ip));
// Default IP if REMOTE_ADDR is invalid
$this_ip = '127.0.0.1';
foreach ($ips as $ip)
{
// check IPv4 first, the IPv6 is hopefully only going to be used very seldomly
if (!preg_match(get_preg_expression('ipv4'), $ip) && !preg_match(get_preg_expression('ipv6'), $ip))
{
// Just break
break;
}
// Use the last in chain
$this_ip = $ip;
}
}
else
{
if (!preg_match(get_preg_expression('ipv4'), $this_ip) && !preg_match(get_preg_expression('ipv6'), $this_ip))
{
$this_ip = '127.0.0.1';
}
}
$e = microtime(true);
$time = round(($e-$s)*1000,6);
echo '"' . $this_ip . '"
'."\n";
echo 'Took: '.$time.' ms
'."\n\n\n";
}
?>