www.webdeveloper.com
Results 1 to 6 of 6

Thread: Ip ban or something like that

  1. #1
    Join Date
    Jun 2009
    Posts
    1

    Ip ban or something like that

    Hello, i need script what ban or prohibit IP for 10 minutes. It can be over MySql too. Please help
    Thank you!

  2. #2
    Join Date
    Apr 2007
    Posts
    1,664
    This function from here claims to get the real IP address of a user.

    PHP Code:
    function getRealIpAddr()
    {
        if (!empty(
    $_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        
    {
          
    $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        
    {
          
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          
    $ip=$_SERVER['REMOTE_ADDR'];
        }
        return 
    $ip;

    Once you have it you'll need to store it along with the time the ban starts in a SQL table. You then need a monitoring script that runs every minute. You can use a cronjob on Unix or a scheduled task on Windows so the job/task calls a script that looks in the table to see is any bans started more than 10 minutes ago and if so remove that record from the database.

    The links provided should give you all the tutorials you need but post back f you need help with any of it.

    If you'd like someone to write it for you this site can help.

  3. #3
    Join Date
    May 2009
    Posts
    21

    resolved re ipban =)

    a script wich bans an user for 10 minutes is commin' up

    if your server don't know the $_SERVER['REMOTE_ADDR'] try the script from SyCo to get the IP address, but i don't think, that this var causes any trouble, cause APACHE and IIS are the most used web servers =)

    and there we go:

    first, we have to create the dbase-table:
    it is not nessasery, witch engine you use, only the fields must exist:

    4 mysql:
    Code:
    CREATE TABLE `ipban`.`ipban` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `ipaddress` VARCHAR( 15 ) NOT NULL ,
    `timestamp` TIMESTAMP NOT NULL ,
    `bantime` TIMESTAMP NOT NULL ,
    `reason` VARCHAR( 255 ) NOT NULL 
    ) ENGINE = MYISAM
    and the PHP-Code

    (watch out for the first to lines in code and fit it to your server)

    PHP Code:
    <?PHP
        
    /**
        *    STREETRULEZ
        **/
        
        
    mysql_connect("localhost""root""");       // connect to a server
        
    mysql_select_db("ipban");  // select the database

        
    ipban_banip($_SERVER['REMOTE_ADDR']); // this will ban the ipadress

        /**
        *    returns false, if ip is not banned
        *    else returns the ip and reason for banning
        *    suüpporting a die-mode XD
        **/
        
    function ipban_checkip($ip$die false)
        {
            
    $res mysql_query("select * from `ipban` where `ipaddress` = '"$ip ."' LIMIT 1;");
            if(
    mysql_num_rows($res) < 1) return false;
            
    $r mysql_fetch_array($resMYSQL_ASSOC);
            if(
    $die)
                die(
    $r['ipaddress'] ." is banned because "$r['reason']);
            else
                return 
    $r['ipaddress'] ." is banned because "$r['reason'];
        }
        
        
    /**
        *    add ip vor time secs with the reason
        *    to the bantable
        **/
        
    function ipban_banip($ip$time 600$reason "you are banned!")
        {
            
    $btime time() + $time;
            
    $btime date("Y-m-d H:i:s"$btime);
            
    mysql_query("insert into `ipban` (`ipaddress`, `timestamp`, `bantime`, `reason`) values ('"$ip ."', NOW(), '"$btime ."', '"$reason ."')") or die(mysql_error());
            return 
    true;
        }
    ?>
    finished XD

    this is simple code =)
    i've wrote that down in 10 minutes using notepad++ and XAMP from apachefriends.org XD

    have phun with it - if it causes troubles feel free to query me some questions by pm or mail m_zell@rocketmail.com


    sorry, bout bad english - i'm from austria and my mothers tounge is german -.-


    so have a nice day,

    maxx
    Last edited by STREETRULEZ; 06-03-2009 at 11:12 AM. Reason: tried to mix english with german language =)

  4. #4
    Join Date
    Apr 2007
    Posts
    1,664
    You'll still need a monitoring script that unbans the ip after 10 mins. The snippet I posted is to get the real ip if it's behind a proxy. Some ISPs put all their customers behind a handful of IPs so by banning the ip of the proxy you ban hundreds or thousands of people.

  5. #5
    Join Date
    May 2009
    Posts
    21

    re ipban

    hi there =)

    i'm sorry to forget 'bout that... i was stressed cause my daughter was born 2 days ago =)

    just rewrite the first function, and there it goes:
    (the whole script ^^)

    PHP Code:
    <?PHP
        
    /**
        *    STREETRULEZ
        **/
        
        
    mysql_connect("localhost""root""");
        
    mysql_select_db("ipban");
        
        
    //ipban_banip($_SERVER['REMOTE_ADDR'], 600);
        //ipban_checkip($_SERVER['REMOTE_ADDR'], 600, true);

        /**
        *    returns false, if ip is not banned
        *    else returns the ip and reason for banning
        *    supporting a die-mode XD
        **/
        
    function ipban_checkip($ip$time 600$die false)
        {
            
    $btime time() + $time;
            
    $btime date("Y-m-d H:i:s"$btime);
            
    mysql_query("delete from `ipban` where `bantime` > '"$btime ."';");
            
    $res mysql_query("select * from `ipban` where `ipaddress` = '"$ip ."' LIMIT 1;");
            if(
    mysql_num_rows($res) < 1) return false;
            
    $r mysql_fetch_array($resMYSQL_ASSOC);
            if(
    $die)
                die(
    $r['ipaddress'] ." is banned because "$r['reason']);
            else
                return 
    $r['ipaddress'] ." is banned because "$r['reason'];
        }
        
        
    /**
        *    add ip vor time secs with the reason
        *    to the bantable
        **/
        
    function ipban_banip($ip$time 600$reason "you are banned!")
        {
            
    $btime time() + $time;
            
    $btime date("Y-m-d H:i:s"$btime);
            
    mysql_query("insert into `ipban` (`ipaddress`, `timestamp`, `bantime`, `reason`) values ('"$ip ."', NOW(), '"$btime ."', '"$reason ."')") or die(mysql_error());
            return 
    true;
        }
    ?>

  6. #6
    Join Date
    Apr 2007
    Posts
    1,664
    Quote Originally Posted by STREETRULEZ View Post
    i'm sorry to forget 'bout that... i was stressed cause my daughter was born 2 days ago =)
    Congratulations! Now shut down the PC and go enjoy you little one

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles