note 104783 added to function.ip2long
| From: | bimal at sanjaal dot com | Date: | Thu, 07 Jul 2011 09:08:33 +0000 |
| Subject: | note 104783 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181305@lists.php.net to get a copy of this message | ||
The signed integers of PHP will produce a lot of negative values for ip2long. Here is what I did it
to make sure that valid numbers enter into my database: INT(10) UNSIGNED.
<?php
# IP is: 192.168.0.224
$ip = sprintf("%u", ip2long(long2ip(ip2long($_SERVER['REMOTE_ADDR']))));
# It produces: 3232235744
?>
I can treat the $ip as string in PHP, while integer for MySQL.
The following works well - while converting back to IP, using the SQL:
SELECT INET_NTOA(3232235744) ip, INET_ATON('192.168.0.224') number;
The results are good: 192.168.0.224 and 3232235744 respectively.
----
Server IP: 69.147.83.197
Probable Submitter: 117.121.234.131
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/104783
Del: integrated -- https://master.php.net/note/delete/104783/integrated
Del: useless -- https://master.php.net/note/delete/104783/useless
Del: bad code -- https://master.php.net/note/delete/104783/bad+code
Del: spam -- https://master.php.net/note/delete/104783/spam
Del: non-english -- https://master.php.net/note/delete/104783/non-english
Del: in docs -- https://master.php.net/note/delete/104783/in+docs
Del: other reasons-- https://master.php.net/note/delete/104783
Reject -- https://master.php.net/note/reject/104783
Search -- https://master.php.net/manage/user-notes.php