View unanswered posts | View active topics It is currently Mon Dec 11, 2017 12:51 am



Reply to topic  [ 1 post ] 
 PHP IP Blocking 
Author Message
Junior Member

Joined: Tue Jul 18, 2006 4:41 am
Posts: 4
Location: India
Post PHP IP Blocking
This tiny article was born after one incident for my web site, which was hacked by a turkey group, so I can't access /read my web site, so I did some coding to redirect the page when the particular IP was detected by PHP code, ok let’s start with general hacking information...


Dealing with hack attempts, evil web bots, and worms has been an ongoing headache.

Most of these problems come from dynamic IP addresses, so simply blocking the offender is only a temporary solution, and we may use to Examining logs and putting blocks in place is time consuming. Remembering to remove blocks on dynamic IP addresses is also a problem.


So we can block particular IP/ranges, even the proxy IP through the following simple code

<?php
$ip_proxy=$_SERVER ["HTTP_X_FORWARDED_FOR"];

?>
The above line refers the predefined variable in PHP $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.



The $_SERVER ['HTTP_X_FORWARDED_FOR'] header giving the IP address of the connection that it proxies, so we use to separate the IP and it's proxy address by using explode function., also we can get the IP address by using $_SERVER['REMOTE_ADDR'] but which is not worth full in web site hosted by sub domain.


I hope you all know about explode function that is Split a string by string

<?php

$tnt=explode (',',$ip_proxy);

$ip=explode ('.',$tnt[0]);

$proxy=explode ('.',$tnt[1]);

?>

next thing is we should block all IP address which are in the text file, so we should enter the IP addresses which are to be blocked , here I have specified some turkey IP address in the text file which doesn’t have full structure of IP address format ,only I took three digit ,you can change code for checking full format of IP address !.

<?php

$filename="input.txt";//text file

$lines = array ();//set as array

$file = fopen ($filename, "r");//Open the file for reading only

while (! feof ($file)) {//read file line by line into a new array element

$lines [] = fgets ($file, 4096); //Gets line from file pointer

}

$x = count ($lines);

for ($y = 0; $y < $x; $y++) {

if((trim($lines[$y])==$ip[0])||(trim($lines[$y])==$proxy[1]))//check the IP/proxy address
{ echo 'Banned';//if IP match the listed IP means ,you can redirect/do some function here .

}

else { echo 'welcome'; }

}

?>

I hope above simple code is useful and learn something about IP blocking, Happy Blocking!!

"


Attachments:
IPB.zip [662 Bytes]
Downloaded 437 times

_________________
"The fear of the Lord is the beginning of wisdom"
Tue Nov 28, 2006 1:00 am
Report this post
Profile ICQ WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.