Whitelist for your project 11-17-2017, 06:32 PM
#1
PHP Whitelist
I just wanted to release something so I thought a little class that allows you to prevent random people from accessing your website might be useful.
I'm kinda too lazy to explain how to use it but it should be fairly easy to understand it by looking at the example usage.
I'd appreciate it if you could submit some ideas for new sinisterly related projects I could work on.
Sincerely,
chunky
Whitelist Class:
Example usage:
I just wanted to release something so I thought a little class that allows you to prevent random people from accessing your website might be useful.
I'm kinda too lazy to explain how to use it but it should be fairly easy to understand it by looking at the example usage.
I'd appreciate it if you could submit some ideas for new sinisterly related projects I could work on.
Sincerely,
chunky
Whitelist Class:
Code:
<?php
/**
* Whitelist
*
*
* @author chunky <chunky@0qare.com>
*/
class Whitelist
{
/**
*
* Class that allows you to prevent non-whitelisted users from accessing your site
*
* @param string $whitelist array of ip adresses you want to whitelist
*/
function __construct($whitelist)
{
$this->whitelist = $whitelist;
if (!empty($_SERVER['HTTP_CLIENT_IP']) && validate_ip($_SERVER['HTTP_CLIENT_IP']))
{
$this->userIP = $_SERVER['HTTP_CLIENT_IP'];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false)
{
$iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($iplist as $ip)
{
if (validate_ip($ip))
{
$this->userIP = $ip;
}
}
} else {
if (validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$this->userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
}
}
if (!empty($_SERVER['HTTP_X_FORWARDED']) && validate_ip($_SERVER['HTTP_X_FORWARDED']))
{
$this->userIP = $_SERVER['HTTP_X_FORWARDED'];
}
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
{
$this->userIP = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
}
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
{
$this->userIP = $_SERVER['HTTP_FORWARDED_FOR'];
}
if (!empty($_SERVER['HTTP_FORWARDED']) && validate_ip($_SERVER['HTTP_FORWARDED']))
{
$this->userIP = $_SERVER['HTTP_FORWARDED'];
}
$this->userIP = $_SERVER['REMOTE_ADDR'];
}
/**
*
* Checks if the user is permitted to access the site
*
* @return boolean
*/
public function hasAccess()
{
if (!empty($this->userIP) && in_array($this->userIP, $this->whitelist))
{
return true;
}
return false;
}
/**
*
* Returns the users ip address
*
* @return string
*/
public function getIP()
{
return $this->userIP;
}
/**
*
* Ensures an ip address is a valid IPv4 address and does not fall within a private network range.
*
* @param string $ipAddress IPv4 address
* @return boolean
*/
private function validateIP($ipAddress)
{
if (strtolower($ipAddress) === 'unknown')
return false;
$ipAddress = ip2long($ipAddress);
if ($ipAddress !== false && $ipAddress !== -1) {
$ipAddress = sprintf('%u', $ipAddress);
if ($ipAddress >= 0 && $ipAddress <= 50331647) return false;
if ($ipAddress >= 167772160 && $ipAddress <= 184549375) return false;
if ($ipAddress >= 2130706432 && $ipAddress <= 2147483647) return false;
if ($ipAddress >= 2851995648 && $ipAddress <= 2852061183) return false;
if ($ipAddress >= 2886729728 && $ipAddress <= 2887778303) return false;
if ($ipAddress >= 3221225984 && $ipAddress <= 3221226239) return false;
if ($ipAddress >= 3232235520 && $ipAddress <= 3232301055) return false;
if ($ipAddress >= 4294967040) return false;
}
return true;
}
}
Example usage:
Code:
<?php
require_once('inc/Whitelist.php');
$whitelist = array('127.0.0.1', '127.0.0.2'); // Array of whitelisted IPv4 addresses
$myWhitelist = new Whitelist($whitelist);
if ($myWhitelist->hasAccess())
{
echo 'Your IP (' . $myWhitelist->getIP() . ') is on the whitelist and you are able to access this site.';
} else {
echo 'Your IP (' . $myWhitelist->getIP() . ') is not on the whitelist and you are not permitted to access this site.';
}