Using PHP to add security with IP filtration/blacklisting.

I remember having issues with an old company I use to work for, always checking my blogs and social profiles trying to get information about what I was currently doing kinda pathetic but true. This of course was due to there paranoia of me retaliating to their unlawful termination, but thats is entirely a new blog post. I thought I would create a security system to protect my personal information and make it a lot more difficult to coordinate the gathering of information.

Traditional methods would say create a login system and set user roles and view access controls but this alone would not be enough. I needed further security that would detect a connections address and then log it. I then would need to create a algorithm that would predict redirects anonymizers and proxies, and then cross refrencing access the logs.

This of course is a very complex way of filtering traffic from your site but one that works.

The following PHP script will detect the IP of a guest and return its value, compares the value’s and then either loads the page if the IP is not on the black list or silently forwards to the site of your choice.
[cc lang=”php”]
// By Miguel Angel Vallejo www.miguelvallejo.com
// This script is Open Source so please use it if you wish. Enjoy! ^_^
// Si nececitas ayuda porfovor dejame un mensaje a mi coreo electronica miguel@miguelvallejo.com

//Lets declare the emtpy variable now.
var public $ip_address = ”;

//Lets create a function called ip_filter() to process our check.
function ip_filter()
{
//This part of the code checks to see if the IP is a Fake
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ip_address = getenv(HTTP_X_FORWARDED_FOR);
} else {
// At this point it has detected that the Ip is masked and it requesting the real IP and storing in inside the variable $ip_address.
$ip_address = getenv(REMOTE_ADDR);
// This is the variable that will store the banned IP address
var private $blacklist = ‘127.0.0.1’;
// Now lets compare the values
if ($ip_address == $blacklist)
{
// if the ip is the same as the blacklisted ip forward the user to:http://www.google.com
header(‘Location:http://www.google.com’);
} else {
return;
}
}
}
[/cc]
Now Lets try something a little more realistic, instead of a single value to compare lets create an array that houses our banned ips, then compare the ip with all values within the array.
[cc lang=”php”]
//IP Detection Script — >
// By Miguel Angel Vallejo www.miguelvallejo.com
// This script is Open Source so please use it if you wish. Enjoy! ^_^
// Si nececitas ayuda porfovor dejame un mensaje a mi coreo electronica miguel@miguelvallejo.com

//Lets declare the emtpy variable now.
var public $ip_address = ”;

//Lets create a function called ip_filter() to process our check.
function ip_filter()
{
//This part of the code checks to see if the IP is a Fake
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ip_address = getenv(HTTP_X_FORWARDED_FOR);
} else {
// At this point it has detected that the Ip is masked and it requesting the real IP and storing in inside the variable $ip_address.
$ip_address = getenv(REMOTE_ADDR);
// This is an array that will store the banned IP addresses
var private $blacklist = array(‘127.0.0.1′,’127.0.0.2’);
// Now lets compare the values, for each value in $blacklist[]
foreach ($blacklist as $value)
{
if($ip_address == $value)
{
header(‘Location:http://www.bing.com’);

}else{
return;
}
}
}
// Initialize the function.
ip_filter();
// load the page default as the security check has cleared as guest not on the banned list.
include(‘default.php’);
[/cc]
Ideally you would place and call the function at the beginning of each page/template which would prevent guests from trying to avoid the check.

If you wanted to take this script further you could tie it to your database where it connects and retrieves the blacklist making things much easier to update with a GUI in the future.
In the next tutorial we will cover taking the IP, time, amount of times and then writing those values into a table in your MySQL database.

If you have any questions please leave them.