Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Tutorials > Tutorials & Articles

Tutorials & Articles The place to comment on the PHP-Editors published tutorials & articles.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2006-11-28, 02:00 AM
sujithfem's Avatar
Junior Member
 
Join Date: Jul 2006
Location: India
Posts: 4
sujithfem is on a distinguished road
Send a message via ICQ to sujithfem Send a message via Skype™ to sujithfem
Default 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 Pending Approval
File Type: zip IPB.zip
__________________
"The fear of the Lord is the beginning of wisdom"
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.5 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.5 (a popular PHP IDE) and NuCoder 2.0 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:26 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.