Page 1 of 1

Comparing IPs...

Posted: Wed Jan 03, 2007 5:54 pm
by Dave2000
I have 2 questions...

1) Which data type should i store IPs as? At the moment i am using VARCHAR. Is this recommended?

2) I have a table with the following rows...

login_id, user_id, ip, success, time

I was wondering how i can select the user_ids that have been logged into by an IP that has also logged into another user_id.

Thank you

Shears :)

Posted: Wed Jan 03, 2007 6:03 pm
by feyd
Well, if you expect to only need to support IPv4 numbers, then an integer will hold it via ip2long().

If on the other hand you want to support IPv6 then the best you can do is a binary encoding, although hex is often preferred for short form.

I've posted IPv6 validation and formating code if you're inclined to support it.

Posted: Wed Jan 03, 2007 11:51 pm
by fractalvibes
Hmm, curious as to why you don't store something in (approximate) "xxx.xx.xx.xx.xx" format as a char. Dunno what the IPv6 format looks like, but suspecting it is similar, though with more nodes.

Seems you would want to deal with as a string?

Enlighten Me!

fv

Posted: Thu Jan 04, 2007 10:48 am
by feyd
IPv4 as a string will automatically use more space than an integer. That is why many store it in octet form.