PHP IP blocking script is not working...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Fran=E7oise_Debat?=

    PHP IP blocking script is not working...

    Hello,

    I wonder if anybody can help. I have an IP blocking script which
    displays a blank screen if an IP is detected from a list in an external
    file. The problem is, the script only reads the last IP¨in the file, not
    every one. Can somebody tell me where this is going wrong?

    <?php

    //get user's IP address
    $userip = $_SERVER['REMOTE_ADDR'];

    //get list of banned IPs from external file
    $filename="http ://www.mysite.com/banned.txt";
    $bannedlist = array();
    $file = fopen($filename , "r");
    while(!feof($fi le)) {

    //read file line by line into a new array element
    $bannedlist[] = fgets($file, 4096);
    }
    fclose ($file);

    //check if the current user's IP is in the banned list
    if (in_array($user ip, $bannedlist))
    {
    //it is, so send him packing
    echo "This website is currently unavailable";
    exit();

    }

    //continue executing the script

    ?>

    The banned.txt file is simple a list of IPs, one per line, as follows:

    82.207.17.160
    84.235.100.2
    87.118.106.177
    193.69.180.120
    84.244.7.168

    I update it using Dreamweaver and upload to my site.

    If anybody could help with this I would be grateful. Thank you

    Françoise
  • Arjen

    #2
    Re: PHP IP blocking script is not working...

    Françoise Debat schreef:
    Hello,
    >
    I wonder if anybody can help. I have an IP blocking script which
    displays a blank screen if an IP is detected from a list in an external
    file. The problem is, the script only reads the last IP¨in the file, not
    every one. Can somebody tell me where this is going wrong?
    <snip long script>

    $banned_list = file($file)
    if (in_array($_SER VER['REMOTE_ADDR'], $bannedlist))
    {
    echo "This website is currently unavailable";
    exit();
    }


    --
    Arjen
    HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

    Comment

    • Heiko Richler

      #3
      Re: PHP IP blocking script is not working...

      Françoise Debat wrote:
      <?php
      >
      //get user's IP address
      $userip = $_SERVER['REMOTE_ADDR'];
      >
      //get list of banned IPs from external file
      $filename="http ://www.mysite.com/banned.txt";
      $bannedlist = array();
      $file = fopen($filename , "r");
      while(!feof($fi le)) {
      >
      //read file line by line into a new array element
      $bannedlist[] = fgets($file, 4096);
      foef is true after the last fgets fails!
      }
      fclose ($file);
      what does print_r($banned list); tell you?
      //check if the current user's IP is in the banned list
      if (in_array($user ip, $bannedlist))
      {
      //it is, so send him packing
      echo "This website is currently unavailable";
      exit();
      >
      }
      >
      //continue executing the script
      >
      ?>
      Do not copy the file in an array. Check row by row. Forget about feof.

      I would do this:

      $file = fopen($filename , "r");
      while($row = fgets($file, 4096)) {
      //read file line by line
      if ($userip == trim($row)) {
      //it is, so send him packing
      echo "This website is currently unavailable";
      exit();
      }
      }
      fclose ($file);


      Heiko
      --
      http://portal.richler.de/ Namensportal zu Richler
      http://www.richler.de/ Heiko Richler: Computer - Know How!
      http://www.richler.info/ private Homepage

      Comment

      • Heiko Richler

        #4
        Re: PHP IP blocking script is not working...

        PS:
        Heiko Richler wrote:
        while($row = fgets($file, 4096)) {
        fgets returns FALSE if an error occurs, so this may be better:

        while(is_string ($row = fgets($file, 4096)))

        or

        while(($row = fgets($file, 4096)) !== false)
        //read file line by line
        Heiko
        --
        http://portal.richler.de/ Namensportal zu Richler
        http://www.richler.de/ Heiko Richler: Computer - Know How!
        http://www.richler.info/ private Homepage

        Comment

        • Kimmo Laine

          #5
          Re: PHP IP blocking script is not working...

          "Arjen" <dont@mail.mewr ote in message
          news:45b7294c$0 $71733$dbd45001 @news.wanadoo.n l...
          Françoise Debat schreef:
          >Hello,
          >>
          >I wonder if anybody can help. I have an IP blocking script which displays
          >a blank screen if an IP is detected from a list in an external file. The
          >problem is, the script only reads the last IP¨in the file, not every one.
          >Can somebody tell me where this is going wrong?
          >
          <snip long script>
          >
          $banned_list = file($file)
          if (in_array($_SER VER['REMOTE_ADDR'], $bannedlist))
          {
          echo "This website is currently unavailable";
          exit();
          }
          That's just one line if you *really* want to optimize (^__^)

          in_array($_SERV ER['REMOTE_ADDR'], file($file))
          and exit("This website is currently unavailable");

          (Kid's, don't try this at home or at all. Writing really short code can be
          quite confusing and very unmaintainable. ..)


          Comment

          • Erwin Moller

            #6
            Re: PHP IP blocking script is not working...

            Kimmo Laine wrote:
            "Arjen" <dont@mail.mewr ote in message
            news:45b7294c$0 $71733$dbd45001 @news.wanadoo.n l...
            >Françoise Debat schreef:
            >>Hello,
            >>>
            >>I wonder if anybody can help. I have an IP blocking script which
            >>displays a blank screen if an IP is detected from a list in an external
            >>file. The problem is, the script only reads the last IP¨in the file, not
            >>every one. Can somebody tell me where this is going wrong?
            >>
            ><snip long script>
            >>
            >$banned_list = file($file)
            >if (in_array($_SER VER['REMOTE_ADDR'], $bannedlist))
            >{
            > echo "This website is currently unavailable";
            > exit();
            >}
            >
            That's just one line if you *really* want to optimize (^__^)
            >
            in_array($_SERV ER['REMOTE_ADDR'], file($file))
            and exit("This website is currently unavailable");
            >
            (Kid's, don't try this at home or at all. Writing really short code can be
            quite confusing and very unmaintainable. ..)
            Kimmo, you MUST love Perl. :P

            Regards,
            Erwin Moller

            Comment

            • =?ISO-8859-1?Q?Fran=E7oise_Debat?=

              #7
              Re: PHP IP blocking script is not working...

              Arjen wrote:
              Françoise Debat schreef:
              >Hello,
              >>
              >I wonder if anybody can help. I have an IP blocking script which
              >displays a blank screen if an IP is detected from a list in an
              >external file. The problem is, the script only reads the last IP¨in
              >the file, not every one. Can somebody tell me where this is going wrong?
              >
              <snip long script>
              >
              $banned_list = file($file)
              if (in_array($_SER VER['REMOTE_ADDR'], $bannedlist))
              {
              echo "This website is currently unavailable";
              exit();
              }
              >
              >
              I'm sorry, I don't understand why, but this solution (none in this
              thread) solved the problem.

              Thanks for the help anyway though :)

              Françoise

              Comment

              • =?ISO-8859-1?Q?Fran=E7oise_Debat?=

                #8
                Re: PHP IP blocking script is not working...

                Heiko Richler wrote:
                PS:
                Heiko Richler wrote:
                >while($row = fgets($file, 4096)) {
                >
                fgets returns FALSE if an error occurs, so this may be better:
                >
                while(is_string ($row = fgets($file, 4096)))
                >
                or
                >
                while(($row = fgets($file, 4096)) !== false)
                >
                > //read file line by line
                >
                Heiko
                Thank you very much! This works beautifully! :))

                Kind regards

                Françoise

                Comment

                • Kimmo Laine

                  #9
                  Re: PHP IP blocking script is not working...

                  "Erwin Moller"
                  <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c omwrote in
                  message news:45b731e2$0 $320$e4fe514c@n ews.xs4all.nl.. .
                  Kimmo Laine wrote:
                  >
                  >"Arjen" <dont@mail.mewr ote in message
                  >news:45b7294c$ 0$71733$dbd4500 1@news.wanadoo. nl...
                  >>Françoise Debat schreef:
                  >>>Hello,
                  >>>>
                  >>>I wonder if anybody can help. I have an IP blocking script which
                  >>>displays a blank screen if an IP is detected from a list in an external
                  >>>file. The problem is, the script only reads the last IP¨in the file,
                  >>>not
                  >>>every one. Can somebody tell me where this is going wrong?
                  >>>
                  >><snip long script>
                  >>>
                  >>$banned_lis t = file($file)
                  >>if (in_array($_SER VER['REMOTE_ADDR'], $bannedlist))
                  >>{
                  >> echo "This website is currently unavailable";
                  >> exit();
                  >>}
                  >>
                  >That's just one line if you *really* want to optimize (^__^)
                  >>
                  >in_array($_SER VER['REMOTE_ADDR'], file($file))
                  > and exit("This website is currently unavailable");
                  >>
                  >(Kid's, don't try this at home or at all. Writing really short code can
                  >be
                  >quite confusing and very unmaintainable. ..)
                  >
                  Kimmo, you MUST love Perl. :P
                  You'd think that, but actually no. I've never learned perl - I haven't even
                  tried it. :)

                  --
                  "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
                  http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
                  spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


                  Comment

                  • Carl Pearson

                    #10
                    Re: PHP IP blocking script is not working...

                    Françoise Debat wrote:
                    Hello,
                    >
                    I wonder if anybody can help. I have an IP blocking script which
                    displays a blank screen if an IP is detected from a list in an external
                    file. The problem is, the script only reads the last IP¨in the file, not
                    every one. Can somebody tell me where this is going wrong?
                    Saw you already had it working, but just thought I'd point out that
                    since you're manually adding the IP's, you could also have done this
                    with .htaccess

                    Not sure if it's any more efficient, just another method. The
                    overhead's got to come from *somewhere*, either the web server parsing
                    the .htaccess file PHP reading your script to check for banned IP's.

                    Comment

                    • Krustov

                      #11
                      Re: PHP IP blocking script is not working...

                      <comp.lang.ph p>
                      <Françoise Debat>
                      <Wed, 24 Jan 2007 10:30:10 +0100>
                      <45b72719$0$259 47$ba4acef3@new s.orange.fr>
                      The banned.txt file is simple a list of IPs, one per line, as follows:

                      82.207.17.160
                      84.235.100.2
                      87.118.106.177
                      193.69.180.120
                      84.244.7.168
                      Theres no need to search a whole list .

                      Convert e.g. 127.0.0.1 to 127_0_0_1

                      $poop=str_repla ce(".","_",$poo p);





                      - grab the live ip address as e.g. $demo


                      $pass=1;

                      $filename="bann ed/$demo";

                      if (!file_exists($ filename)) {$pass=0;}

                      if ($pass==0) {exit();}


                      In effect , You could have 10,000 banned ip addresses and the above
                      method would only do a single look up search instead of searching a
                      10,000 list of ip addresses .


                      --

                      (work in progress)

                      Comment

                      Working...