Tag: php ip filter script

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.

12345678910111213141516171819202122232425262728

// 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.varpublic$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 Fakeif(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 addressvarprivate$blacklist='127.0.0.1';// Now lets compare the valuesif($ip_address==$blacklist){// if the ip is the same as the blacklisted ip forward the user to:http://www.google.comheader('Location:http://www.google.com');}else{return;}}}

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.

//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 Fakeif(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 addressesvarprivate$blacklist=array('127.0.0.1','127.0.0.2');// Now lets compare the values, for each value in $blacklist[]foreach($blacklistas$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');

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.