Stupid PHP tricks
Jul. 19th, 2012 04:59 pmSo, I'm trying to write some stuff in PHP, and I've run into a thing I can't seem to figure out how to do. I want to take two IP addresses (in dotted decimal format) and a CIDR mask, and see if IP-1 is contained within IP-2/mask.
I've been poking around to see if this is an obviously solved problem - and I would think it must be, because it's not an unusual problem, but I can't seem to find it. And trying to script this myself is causing me to go cross-eyed trying to figure out how to do binary math without the ability to cast a variable as a binary. (Really, PHP? Really?)
I suppose one solution is to go get a real language, or maybe wait until phpv6. But, still, for something that's allegedly optimized for web-based stuff, I'm really shocked there doesn't seem to be much in the way of built-in IP-wrangling stuff besides, say, "ip2long" which will take a dotted decimal format IPv4 addr and turn it into a long integer.
I've been poking around to see if this is an obviously solved problem - and I would think it must be, because it's not an unusual problem, but I can't seem to find it. And trying to script this myself is causing me to go cross-eyed trying to figure out how to do binary math without the ability to cast a variable as a binary. (Really, PHP? Really?)
I suppose one solution is to go get a real language, or maybe wait until phpv6. But, still, for something that's allegedly optimized for web-based stuff, I'm really shocked there doesn't seem to be much in the way of built-in IP-wrangling stuff besides, say, "ip2long" which will take a dotted decimal format IPv4 addr and turn it into a long integer.