Comparing IPs...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Comparing IPs...

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

IPv4 as a string will automatically use more space than an integer. That is why many store it in octet form.
Post Reply