ip2long, 32-bit/64-bit

It is a common practice, to use ip2long() function for getting integer-representation of ip-address (for database-storage).

Problem:

on systems with 32-bit integers, ip2long() returns values from -2147483648 to 2147483647

on systems with 64-bit integers, ip2long() returns values from 0 to 42949672945

Which means, that if you were using INT as a storage type on 32-bit server, you will need to change that field to be UNSIGNED INT (or BIGINT) on 64-bit server.

There is no simple migration, sorry.

p.s. similiar problems can appear in other intger-related functions too

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • FriendFeed
  • Reddit
  • Tumblr
  • Twitter

View Commentsip2long, 32-bit/64-bit

  • Actually, it is clearly shown in doc for ip2long that “you need to use the “%u” formatter of sprintf() or printf() to get the string representation of the unsigned IP address.”

  • I know. But signed long was just fine for me. I understand why it has changed.

    The problem is: people tend to think about PHP as about high-level language (and hope, that it will take care of such interoperability issues).
    But such examples show, that many parts of it are still low-levelish

  • I always use built-in Mysql function INET_ATON() and have no problems :) Of course, the point is i never use other DB than Mysql

  • jeteir

    Which means, in fact, that PHP SHOULD deal with it by itself.

    It’s not a pretty thing for language that supports :// wrappers to halt on this.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

blog comments powered by Disqus