Skip to content

Instantly share code, notes, and snippets.

@Gkiokan
Created March 21, 2026 21:28
Show Gist options
  • Select an option

  • Save Gkiokan/9fa6feb02c98fc47db08585d666e66a1 to your computer and use it in GitHub Desktop.

Select an option

Save Gkiokan/9fa6feb02c98fc47db08585d666e66a1 to your computer and use it in GitHub Desktop.
IP Service Check Class
<?php
/**
Author: Gkiokan Sali
Date: 2026-03-21
Comment: Check if the given IP is in the allowed Ips List or CIDR Range via Subnet.
**/
class IPService
{
public function check($ip){
$allowedIps = ['127.0.0.1', '::1', 'localhost', '195.0.0.0/8', '172.16.0.0/12', '10.0.0.0/8'];
$requestIP = $ip;
if (!$this->isIpAllowed($requestIP, $allowedIps)) {
printf("Password Reset Request for is not on the allowed IP Range {$requestIP}");
}
print("Allowed?");
}
private function isIpAllowed($ip, $allowedList)
{
foreach ($allowedList as $allowed) {
echo "\n[0] isIpAllowed Test for $allowed \n";
if (strpos($allowed, '/') !== false) {
// CIDR notation
if ($this->ipInCidr($ip, $allowed)) {
return true;
}
} else {
// Single IP
if ($ip === $allowed) {
return true;
}
}
}
return false;
}
private function ipInCidr($ip, $cidr)
{
list($subnet, $mask) = explode('/', $cidr);
echo "-[1] ip in CIDR $subnet $mask \n";
if ((filter_var($subnet, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) &&
(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))) {
// IPv4
$ipLong = ip2long($ip);
$subnetLong = ip2long($subnet);
$maskLong = -1 << (32 - $mask);
$subnetLong &= $maskLong;
echo "--[2] $ipLong, $subnetLong, $maskLong, $subnetLong \n";
echo "--[2] compare " . ($ipLong & $maskLong) . " = " . $subnetLong . "\n";
return ($ipLong & $maskLong) == $subnetLong;
}
return false;
}
}
// Usage
$do = new P();
$do->sendPasswordResetMailFromInternal('195.201.160.141');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment