note 83548 added to function.ip2long
| From: | chrisp-phpnet at inventivedingo dot com | Date: | Sat, 31 May 2008 02:36:14 +0000 |
| Subject: | note 83548 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140023@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:
$ip = htmlspecialchars($_SERVER['REMOTE_ADDR']);
if (strpos($ip, '::') === 0) {
$ip = substr($ip, strrpos($ip, ':')+1);
}
$host = ip2long($ip);
Ugly but functional.
----
Server IP: 203.88.112.146
Probable Submitter: 203.32.87.50
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/83548
Del: integrated -- https://master.php.net/note/delete/83548/integrated
Del: useless -- https://master.php.net/note/delete/83548/useless
Del: bad code -- https://master.php.net/note/delete/83548/bad+code
Del: spam -- https://master.php.net/note/delete/83548/spam
Del: non-english -- https://master.php.net/note/delete/83548/non-english
Del: in docs -- https://master.php.net/note/delete/83548/in+docs
Del: other reasons-- https://master.php.net/note/delete/83548
Reject -- https://master.php.net/note/reject/83548
Search -- https://master.php.net/manage/user-notes.php