note 83548 modified in function.ip2long by danbrown
| From: | danbrown@php.net | Date: | Fri, 05 Dec 2008 13:26:39 +0000 |
| Subject: | note 83548 modified in function.ip2long by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147681@lists.php.net to get a copy of this message | ||
I had a problem with calling this function with REMOTE_ADDR on my lighttpd web server. Turned out
that IPv6 was installed on this server, so even though REMOTE_ADDR was an IPv4 address, it was being
formatted using IPv6's IPv4 compatibility mode. For example, 10.0.0.1 was becoming
::ffff:10.0.0.1, which caused iplong to report the address as invalid instead of parsing it
correctly.
The correct fix would of course be to update my infrastructure so that it's properly
IPv6-compatible; but in the context of my particular situation, this would involve a lot of
re-engineering. So in the meantime, I worked around the issue using this quick and dirty hack:
<?php
$ip = htmlspecialchars($_SERVER['REMOTE_ADDR']);
if (strpos($ip, '::') === 0) {
$ip = substr($ip, strrpos($ip, ':')+1);
}
$host = ip2long($ip);
?>
Ugly but functional.
--was--
I had a problem with calling this function with REMOTE_ADDR on my lighttpd web server. Turned out
that IPv6 was installed on this server, so even though REMOTE_ADDR was an IPv4 address, it was being
formatted using IPv6's IPv4 compatibility mode. For example, 10.0.0.1 was becoming
::ffff:10.0.0.1, which caused iplong to report the address as invalid instead of parsing it
correctly.
The correct fix would of course be to update my infrastructure so that it's properly
IPv6-compatible; but in the context of my particular situation, this would involve a lot of
re-engineering. So in the meantime, I worked around the issue using this quick and dirty hack:
$ip = htmlspecialchars($_SERVER['REMOTE_ADDR']);
if (strpos($ip, '::') === 0) {
$ip = substr($ip, strrpos($ip, ':')+1);
}
$host = ip2long($ip);
Ugly but functional.
http://php.net/manual/en/function.ip2long.php