PHP-IP Block 08-26-2013, 04:26 PM
#1
For simple IP address blocking, there are simpler ways, for example htaccess
Behind this script, there is a need to deal with more subtle way.
For example, display different content for different users without that they will
find themselves losing any of the content of the page.
Example of this could be the page of a company, which is known to a competitor
using a static IP or IP range and want to show them only the the part of
the site's content.
Although his is not impervious solution,
and that's why this is not a proper way of handling security solutions
but in some cases handy.
Think about situation where employee of the competitor visits your website from
his/her workplace and doesn't see, because of the block, that information
is concealed from him/her. Probably she/he isn't going to visit website on her/his
computer at home when she/he doesn't know that information was concealed from
her/him.
Nothing very classified this doesn't hide but making spying harder,
this is useful.
Although classified data could be stored in password-protected directory,
but if you want that data is displayed for potential customers,
it's out of the question.
IP addresses which should be blocked could be in this case individual static IPs
so that's why we're handling addresses this accurately and not
arbitrarily blocking all the 192.168 range addresses, which would be easier.
The code is intentionally in the form of a function, so it's easy to attach
to a part of your code.
Behind this script, there is a need to deal with more subtle way.
For example, display different content for different users without that they will
find themselves losing any of the content of the page.
Example of this could be the page of a company, which is known to a competitor
using a static IP or IP range and want to show them only the the part of
the site's content.
Although his is not impervious solution,
and that's why this is not a proper way of handling security solutions
but in some cases handy.
Think about situation where employee of the competitor visits your website from
his/her workplace and doesn't see, because of the block, that information
is concealed from him/her. Probably she/he isn't going to visit website on her/his
computer at home when she/he doesn't know that information was concealed from
her/him.
Nothing very classified this doesn't hide but making spying harder,
this is useful.
Although classified data could be stored in password-protected directory,
but if you want that data is displayed for potential customers,
it's out of the question.
IP addresses which should be blocked could be in this case individual static IPs
so that's why we're handling addresses this accurately and not
arbitrarily blocking all the 192.168 range addresses, which would be easier.
The code is intentionally in the form of a function, so it's easy to attach
to a part of your code.
Code:
<?php
/*
Parameters of function:
1: $aip = Beginning of the ip area is '123.0.0.0'
2: $bip = End of the ip area is '123.255.255.255' or empty
(if the case of individual ip, empty)
3: $xip = compared ip is form of '123.1.2.3 or empty
(if nothing else if given, clarify the ip of visitor)
4: $required = is ip address compulsory, true or false
(in the case it's not clarified)
Function returns value TRUE if ip address is compulsory and it is cleared
and it doesn't match with given comparison address or it isn't in the given address range
*/
function blockIps($aip=false,$bip=false,$xip=false,$required=true) {
// Determine the compared ip address, if it's not given
if (!$xip) {
if (getenv("HTTP_CLIENT_IP")) {
$xip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("HTTP_X_FORWARDED_FOR")) {
$xip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("REMOTE_ADDR")) {
$xip = getenv("REMOTE_ADDR");
}
else {
$xip = false;
}
}
// Address was not discovered, but it's not required = no
if ($required && !$xip) {
return false;
}
// Addres was not discovered, but it is not required = ok
elseif (!$required && !$xip) {
return true;
}
// IP address determined and the point of reference is an individual address
if ($xip && $aip && !$bip) {
$xip = sprintf('%u', ip2long($xip));
$aip = sprintf('%u', ip2long($aip));
// Given addresses don't match = ok
if ($xip!=$aip) {
return true;
}
}
// Ip address determined and the point of reference is a certain ip address area
elseif ($xip && $aip && $bip) {
$xip = sprintf('%u', ip2long($xip));
$aip = sprintf('%u', ip2long($aip));
$bip = sprintf('%u', ip2long($bip));
// Address is not in the field = ok
if ($xip<$aip || $xip>$bip) {
return true;
}
}
// Default = no
return false;
}
?>
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)