Download the PHP package jaaulde/php-ipv4 without Composer
On this page you can find all versions of the php package jaaulde/php-ipv4. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-ipv4
php-ipv4
PHP classes for working with IPV4 addresses and networks.
Install
via composer
Change log
Please see CHANGELOG for information about what has changed recently.
Testing
License
The MIT License (MIT). Please see License File for more information.
Usage
...
Examples
_Warning: These examples are simplified and without context. Usage of unfiltered/unvalidated request variables (such as from $_POST
) in these examples is not an endoresement or recommendation to do so in your own projects._
- Hard Coded Whitelist
- DB Driven Address Blocking
Hard Coded Whitelist
DB Driven Address Blocking
Many PHP applications allow administrators to blacklists for ranges of abusive IPs to ease moderator work loads. Most of these applications use overly complicated db schemas to store the IP addresses which represent the ranges, and use a series of string manipulations to determine if IPs fall into the banned ranges. By applying the actual math and logic involved in the IPv4 address scheme, php-ipv4
radically simplifies all of this, and provides a more reliable filtering of IPs.
An example administrator interface may offer a form something like this for entering ranges to ban:
The receiving PHP script, ip-ban.php
would then do something like this:
Supposing the Range
instance was properly created, we could move on to store it. This is where php-ipv4
really begins to stand out. Rather than store some string representation of the IP in a database for later retrieval and comparison gyrations, php-ipv4
lets us easily store the integer representation of the IP in database columns for later querying using a BETWEEN
clause.
Given a database table with integer values representing the first and last IP addresses in blocked ranges:
We could ask the range for the integer value of its first and last addresses and store those.
Finally, when we want to check if a client's IP address is one which is blocked, we can simply query like so:
If the above query returns a count greater than 0, you know that the client's IP address is on a ban list. Otherwise, they're allowed to continue. It's that simple--there is no need ask for the actual data in the rows or iterate and compare.
That's not all, though. A better admin interface would allow you to specify any IP and a subnet mask in dot notation or CIDR. php-ipv4
can make that a breeze as well. Given the same DB schema as before and a form that would POST address
and mask
to ip-ban.php
, you could simply:
The check for whether a client IP was blacklisted would then be identical to the already given example for such.