note 87140 added to function.ip2long
| From: | jbezorg at gmail dot com | Date: | Thu, 20 Nov 2008 22:04:58 +0000 |
| Subject: | note 87140 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146782@lists.php.net to get a copy of this message | ||
Had a need to convert IPs stored in VARCAR(15) to UINT in an old PHP application. Here are my tests
for converting in a MySQL database so you don't have to reinvent the wheel.
#Dotted format to INT in MySQL
SET @ip_as_string = '198.128.128.0';
SELECT
( SUBSTRING_INDEX( @ip_as_string, '.', 1 )<<24 ) +
( SUBSTRING_INDEX( SUBSTRING_INDEX( @ip_as_string, '.', 2 ), '.', -1
)<<16 ) +
( SUBSTRING_INDEX( SUBSTRING_INDEX( @ip_as_string, '.', -2 ), '.', 1 )<<8
) +
SUBSTRING_INDEX(@ip_as_string, '.', -1)
IPvINT;
#INT to dotted format in MySQL
SET @ip_as_long = 3330310144;
SELECT
CONCAT( @ip_as_long>>24, '.', (@ip_as_long&16711680)>>16, '.',
(@ip_as_long&65280)>>8, '.' , @ip_as_long&255 ) IPv4;
----
Server IP: 64.71.164.2
Probable Submitter: 148.129.74.42
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/87140
Del: integrated -- https://master.php.net/note/delete/87140/integrated
Del: useless -- https://master.php.net/note/delete/87140/useless
Del: bad code -- https://master.php.net/note/delete/87140/bad+code
Del: spam -- https://master.php.net/note/delete/87140/spam
Del: non-english -- https://master.php.net/note/delete/87140/non-english
Del: in docs -- https://master.php.net/note/delete/87140/in+docs
Del: other reasons-- https://master.php.net/note/delete/87140
Reject -- https://master.php.net/note/reject/87140
Search -- https://master.php.net/manage/user-notes.php