Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to convert ip to integer in php?

Tags:

php

ip

So I need to store IP addresses in database, but storing them as stings is not very efficient and not very convenient for my purpose.

So... How can I convert ip into integer in php with as less processing as possible because it will be done millions of times a day.

And of course how can I convert back from integet to ip?

I know this can be googled and there's some easy solutions - but I'm asking for the fastest way, not just for "do X and you get Y" because it's actually pretty easy task.

like image 823
NewProger Avatar asked Jun 27 '11 20:06

NewProger


1 Answers

Use ip2long() and long2ip(). They are the fastest you can find in PHP, because they are just built on top of the corresponding C functions.

like image 71
KingCrunch Avatar answered Oct 06 '22 10:10

KingCrunch