Search Code

  Home arrow Search Code arrow PHP IP Blocking
SEARCH CODE

PHP IP Blocking
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2006-11-28

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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


    By : sujithfem

    <?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!!

    "


    Click to Download File



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Search Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    SEARCH CODE ARTICLES

    - PHP IP Blocking
    - Search engine with ranking feature
    - Ajax access to Google API
    - Table Searcher
    - Search_It
    - FTP SEARCH
    - Search and Replace class
    - Simple Search on mysql database
    - Search Your Database
    - search_v1.1
    - Database Searching Class

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap