Network Functions

This file contains multiple functions. I normally avoid having multiple functions in a scriptlet. However they were so closely intertwined that it just made sense. These functions basically accomplish an IP calculator for PHP. They include functions to turn an IP into a binary string, and vice-versa along with functions for calculating the network and broadcast address of a particular network. There’s also a function to check whether a particular IP exists in a given network.

I’ll admit readily that I’d much rather have used binary arithmetic to work these functions. After all that would make sense. However PHP unfortunately does not support unsigned integers (at this time and I could find no reference to them extending their variable capabilities.) As a result, these functions are a slight bit slower than I would like. They have to do a string-based binary comparison to ensure that all IPs in the top end of the 32bits are covered. Some additional performance improvements could be made in two regards right now, but they aren’t of priority.

  • Add a function to detect the submitted parameter so that in_network could avoid doing the binary calculation on the netmasks three times (once in subcalls to broadcast() and network() and of course once directly to get the netmasks’s binary equivalent.
  • Change the whole thing into a class that caches its previous calculations for subsequent calls.

Of course, when I do code these features I’ll update this page.

Example:

 

Leave a Reply

Your email address will not be published. Required fields are marked *