Skip to content

Instantly share code, notes, and snippets.

@Elshaden
Created June 24, 2020 16:40
Show Gist options
  • Save Elshaden/9f1b689ed6d3cf0f105b4c9020417a03 to your computer and use it in GitHub Desktop.
Save Elshaden/9f1b689ed6d3cf0f105b4c9020417a03 to your computer and use it in GitHub Desktop.
IP white List
<?php
namespace App\Containers\Authentication\Actions;
use Symfony\Component\HttpFoundation\IpUtils;
class IpWhiteList
{
/**
* A Class to Validate request IP against array of IP's
* @return bool
*/
public function checkIP(){
// White List Ip's
$ips = ['x.x.x.x','x.x.x.x'];
return IpUtils::checkIp($this->getIp(), $ips);
}
/**
* @return mixed|null
*/
private function getIp(){
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if (isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if (isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if (isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = null;
return $ipaddress;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment