rockinaway Posted September 11, 2011 Share Posted September 11, 2011 I've been doing some research and I was wondering what the best way is to store IP addresses in a MySQL database, and what would be the field type and length? I've seen that you can use the inet_aton() function but supposedly this doesn't support IPv4. So is there a workaround using these functions? Alternatively, if I was to just store the IP address as it is, what type of field and length would I be looking at? I've read somewhere that 45 is the max? Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/ Share on other sites More sharing options...
Pikachu2000 Posted September 11, 2011 Share Posted September 11, 2011 INET_ATON() supports IP V4. It doesn't support IP v6. IP v6 is a hexadecimal number. Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268014 Share on other sites More sharing options...
rockinaway Posted September 11, 2011 Author Share Posted September 11, 2011 Oh, well which method would you suggest to store IP addresses? Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268017 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 You could store it in an unsigned int column (should be big enough) and use sprintf("%u", ip2long($ip)); This will save a bunch of space over a string. Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268047 Share on other sites More sharing options...
rockinaway Posted September 11, 2011 Author Share Posted September 11, 2011 Please can you explain the reasoning for the space thing? Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268049 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 Storing an int takes up 4 bytes. Storing a string takes 1 byte per character. If the string is over 4 characters, it will take up more space than an int. Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268052 Share on other sites More sharing options...
rockinaway Posted September 11, 2011 Author Share Posted September 11, 2011 So what does the function you have given, do exactly? Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268054 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 Are you too short to grab that manual from the top of the shelf? http://ca.php.net/manual/en/function.ip2long.php Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268059 Share on other sites More sharing options...
rockinaway Posted September 11, 2011 Author Share Posted September 11, 2011 No, i've just spent hours reading over different stuff and my brain is mangled.. Now, does that mean it ONLY handles IPv4? Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268078 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 "ip2long — Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address" 'Tis would seem so. Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268163 Share on other sites More sharing options...
rockinaway Posted September 12, 2011 Author Share Posted September 12, 2011 Yes, so which method is best to support BOTH IPv4 and IPv6? Just storing them as the characters that they are? Quote Link to comment https://forums.phpfreaks.com/topic/246912-best-way-to-store-ip-address-in-database/#findComment-1268257 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.