3 ms·
I use this to store IP addresses in a database because you can operate on numbers (e.g. WHERE subnet_start < $thisip < subnet_end) but hardly on the unique dott
by lucb1e 4y ago
I use this to store IP addresses in a database because you can operate on numbers (e.g. WHERE subnet_start < $thisip < subnet_end) but hardly on the unique dotted format that we normally display them as.
Also to specify a bind address when I don't care, like running `php -S 0:3000` (the silly thing wants a bind address rather than only a port number. There, have one!) or accessing localhost in a browser (just typing 0:3000 is enough). For 127.0.0.1, unfortunately the best you can do is writing 127.1. The numeric, hex, and octal variants are 2130706433, 0x7f000001, and 017700000001, which I personally don't find preferable to 127.1.
- crazygringo 4y agoYes, storing is different from displaying though. In MySQL for example, that's what INET_ATON() and INET_NTOA() are for, to convert between binary and display. Analogous to storing timestamps but displaying as datetimes in a timezone.
- lucb1e 4y agoI'd still much rather store something that can be indexed in a btree than something where you have to always call a function on and do full table scans. Of course, before displaying to the user you'd use long2ip again (or the database equivalent you mentioned; I usually avoid doing unnecessary computations on the database and, instead, let the application handle display logic).
- crazygringo 4y agoSorry if I wasn't clear, I was agreeing with you! Yes precisely for indexing (plus just a fixed column size that wastes no space).
- wjholden 4y agoThis is why I love this site. I've been doing networking for many years and I'm not a total novice in databases, but I had no idea MySQL had these functions.
- brobinson 4y agoPrepare to be even more amazed: https://www.postgresql.org/docs/current/datatype-net-types.html https://www.postgresql.org/docs/current/datatype-net-types.h... https://www.postgresql.org/docs/current/functions-net.html https://www.postgresql.org/docs/current/functions-net.html
- chrisdhal 4y agoIf you're using a database, use something like Postgres that has this functionality built in[0]. You can store IPs, networks, etc. in a native format that has all sorts of functionality available out of the box. [0] https://www.postgresql.org/docs/current/functions-net.html https://www.postgresql.org/docs/current/functions-net.html