Keverw
Keverw

Reputation: 3786

Store and search IPv6 ranges in PHP MySQL

I have a CVS database which gives a V6 range 2001:1800::/32 and a country code US. So far I have them in $ip_address $cc_code.

I need to store each range in MySQL. I also need take a user IP address and get the country code. I already got V4 working by storing a start and end range but the database already gave me that. So I'm stuck on getting V6 to work. I did some Google searches and can't find much on this. Maybe I'm asking wrong? So can someone here give me some advice? I already know how to detect V4 and V6. So I have lots of it done so far. Just storing range/code and retiring it where i'm stuck.

Upvotes: 2

Views: 1209

Answers (2)

jaggedsoft
jaggedsoft

Reputation: 4038

MySQL 5.6.3 introduced the following new functions to work with IPv6 addresses:

INET6_ATON() Return the numeric value of an IP address

INET6_NTOA() Return the IP address from a numeric value

For example, hex(INET6_ATON('fdfe::5a55:caff:fefa:9089')) returns FDFE0000000000005A55CAFFFEFA9089

You can store IPv6 in your database as VARBINARY(16)

Upvotes: 0

Vilius Sutkus '89
Vilius Sutkus '89

Reputation: 804

IP addressed are basically Integers, you just need to convert them and the interact with basic math skills: WHERE $ip BETWEEN start AND end. http://www.samclarke.com/2011/07/php-ipv6-to-128bit-int/

Upvotes: 4

Related Questions