https://github.com/jalle19/php-whitelist-check
Very light-weight library for checking if a client is on a whitelist
https://github.com/jalle19/php-whitelist-check
cidr php whitelist
Last synced: 6 months ago
JSON representation
Very light-weight library for checking if a client is on a whitelist
- Host: GitHub
- URL: https://github.com/jalle19/php-whitelist-check
- Owner: Jalle19
- License: bsd-2-clause
- Created: 2014-01-10T11:58:02.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2023-08-31T11:59:25.000Z (about 2 years ago)
- Last Synced: 2025-04-03T01:12:59.371Z (7 months ago)
- Topics: cidr, php, whitelist
- Language: PHP
- Homepage:
- Size: 57.6 KB
- Stars: 40
- Watchers: 6
- Forks: 19
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/Jalle19/php-whitelist-check/actions/workflows/tests.yml)
[](https://coveralls.io/github/Jalle19/php-whitelist-check?branch=master)
[](https://scrutinizer-ci.com/g/Jalle19/php-whitelist-check/?branch=master)php-whitelist-check
===================A modern and simple approach to validating IP addresses and domains against a whitelist. It supports both IPv4 and IPv6 addresses and CIDR subnets in addition to domain names and wild-card domains.
## Requirements
* PHP 5.3 or newer
## Usage
The `Check::whitelist()` method takes an array of definitions which will constitute the whitelist. The definitions can either be strings (which will be parsed to their respective objects) or objects.
The `Check::check($value)` method is used to check the specified value against the current whitelist. The method will return true if the value matches any of the definitions.
To create your own definition classes just extended `Whitelist\Definition\Definition` and implement `Whitelist\Definition\IDefinition`
Example usage:
```php
require_once("vendor/autoload.php");$checker = new Whitelist\Check();
try {
$checker->whitelist(array(
'10.0.3.1',
'10.0.0.0/16',
'2001:db8:100:934b::3:1',
'2001:db8:100:934b::/64',
'*.example.com',
'localhost',
new Whitelist\Definition\Domain('vpn.work.com'),
));
}
catch (InvalidArgumentException $e) {
// thrown when an invalid definition is encountered
}$checker->check('10.0.1.1'); // true
$checker->check('10.1.1.1'); // false
$checker->check('2001:db8:100:934b::210:2'); // true
$checker->check('another.example.com'); // true```
## License
This library is licensed under the BSD 2-Clause License
## Credits
This library depends on `xrstf/ip-utils` for the IP-related functionality. It also assumes that ip-utils's test cases are sufficient, which is why only trivial testing on these functions have been made for this library.