To IP or not to IP

I’ve been thinking about this for a site that I am doing and just can’t firgure which way is better, so I’m hoping the community can give me some ideas.

Depends on what you are concerned storing size vs machine time

iptolong pro:

storing size is much smaller, I didn’t find the range value that is generated by iptolong but I think an unsigned int (4 byte) should be fine

iptolong cons:

does not support IPV6, so do not use it unless you want to recode in a relative short time

you need to convert the ip before storing

you need to convert back the ip before searching and before showing the result to the user

direct query on the db are more difficult (when you use any db client to inspect your data)

Not sure but if is possible to query over ip range (depend on the results)

you cant use INET6_ATON / INET6_NTOA mysql function (these function are avail for other db) to query for specific range

string store pro:

no cpu needed to convert the value

you can query “quirk” over ip range, like “where ip like ‘192.168.1.%’” (with some limit obviously)

you can do real range query with INET6_ATON / INET6_NTOA db function:

SELECT * FROM access_log WHERE ip_client BETWEEN inet_aton(‘’) AND inet_aton(‘’);


string store cons[/b]:

bigger data, to store an ip as string you need a varchar(15) which means

minimum ( 7+1 byte

maximum ( 15+1 byte


Even in the better case the string storing space is the double than iptolong.

Very often I do calculation over db size and most of the time I chose the easy way for coding over the space saving.

Some time I take in account also cpu time but this is the last of my problem usually

Let me explain:

CPU time:

When we go soo depp on the code most of the time we speak about 0,0001 second vs 0,0004

so this means that after 1000 request you saved 3 second of cpu

But nowadays cpu idle most of the time, if your application have 100 concurrent access all the time your server is still ok

If your reach 500 concurrent access maybe you start to have some problem, but this also means that probably you have at least (without going in deep performance test calculation for which visit log are needed) around 2 millions unique visitors per day. This number obviously depend on the application type, it can be bigger, it can be smaller.

With so many visitor you should have enough money to buy additional cpu.

Other aspect to consider, the user experience. In reality this is the most relevant part, if your page is slow the people get upset and do not come anymore to you site.

0,0003 second of difference is not relevant for user.

Also when you have long task (which usually happen in business application) you should always take in account how many time this task is running.

Premise everything can be put in queue and scheduled for background.

15 minute task, once per month is not relevant while daily it start to be a problem

5 minute task once per day is ok, but not more than once

1 minute task per hour is not ok while 10 second maybe is ok

[b]Storage Space:

[/b]for the storage space are valid same consideration as above for the number of visitors.

When you will have storage problem there are 2 possibility:

[list=1][]your application is running form a long long time, so is the moment to backup and delete your older data[]you have a lot of visitor so you can afford more storage space[/list]


if you still want to go for php function use inet_ntop/inet_pton which are ipv6 ready

Thanks for your well thought out response Roberto. I was trying to figure out which way (number vs. string) would be better. It sounds like the answer is Yes :blink:

Looking at the Yii2 IpValidator, I think string storage would be the way to go. I just need to figure out how a related field/attribute as part of the rules[] for checking if IP is in the CIDR range. (ie. The IP I assign to an item is actually in the subnet.)