Web Analytics Made Easy -
StatCounter Best way for a banning script - CodingForum

Announcement

Collapse
No announcement yet.

Best way for a banning script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best way for a banning script

    Does anyone have a good banning script?

    For now I'm using this is lots of my (test) scripts:
    Code:
    @BANNED_IP=("127.0.0.1","10.0.0","45");
    $visitors_ip = $ENV{REMOTE_ADDR};
    foreach (@BANNED_IP) {
    	if ($visitors_ip =~ /^$_/i) {
    		&nice_die("$banned_message");
    	}
    }
    But, I'm sure there's a much better way, since this script goes through all the IPs in @BANNED_IP and takes some time to execute if the list gets longer.

    I hope I'm clear to you

    Thanks in advance;
    Mzzl, Chris
    My Website
    010100010011110101110100011011110111000001101000

  • #2
    Are you sure it'll take that long to go through...?

    Just as a test... on my machine it took under a second pretty much to loop through 1 000 000 values in an array and compare them to a number. I doubt you'll want to ban that many users hehe .

    The script I used was this by the way:
    Code:
    #!/usr/bin/perl
    
    for ($i = 0; $i < 1000000; $i++) {
    push (@numbers, int rand 1000);
    }
    
    $bftime = time();
    print "Starting...\n";
    foreach (@numbers) {
    if (/^387$/) {
    print "found one...\n";
    }
    }
    $aftime = time();
    
    print "Done..\n";
    print "Time taken was: ", $aftime - $bftime, " seconds\n";
    I think the way you've got of doing it will work just fine...

    Comment


    • #3
      On the other hand my machine is a 1.2Ghz Athlon... .

      Comment


      • #4
        But isn't there just a function to check if a value is in an array; for example:

        I have a value $my_favorite containing fish and I have an array @food containing ("bread","apple","steak","patatoos","fish","icecream","vegitebles") (I don't know how to write some, but I hope you get the idea)
        Now lets say the function I'm talking about is named where_is() and I use the next line:
        $position = where_is($food,$my_favorite);
        Then I want $position to be 4

        Get it?

        Thanks, Chris

        Edit: Forgot a [/b]
        My Website
        010100010011110101110100011011110111000001101000

        Comment


        • #5
          Nope, nothing like that... you'd have to make your own function, something like:

          Code:
          sub where_is {
          my ($array, $what) = @_;
          my ($i);
          
          for ($i = 0; $i < scalar @$array; $i++) {
          return $i if $what =~ /^$$array[$i]$/;
          }
          
          return -1;
          }
          That's assuming you're passing an array reference to the subroutine, rather than the array itself... i.e you'd call it with

          print where_is (\@food, $my_favourite);

          Rather than

          print where_is (@food, $my_favourite);

          Seriously - don't worry about your code - it's quite a common thing to do, really, and it really does use up less CPU time than you might think.

          Perl isn't like PHP where there's a function for anything and everything, it gives you what you need language-wise, and you do the rest

          Comment


          • #6
            Ok, so there's no function what does what the function you made does...

            Well, then I'll start using yours, thank you very much.

            Mzzl, Chris

            P.S. what does scalar do and why is the $ after ^ at /^$$array[$i]$/
            My Website
            010100010011110101110100011011110111000001101000

            Comment


            • #7
              The 'scalar' forces the array to be interpreted in a scalar context - i.e. it returns the number of elements in the array. With arrays you can often use $#array to return the last element in the array, but I was uncertain about using this on an array reference.

              The regular expression /^$$array[$i]$/ isn't too bad.

              The ^ at the front and the $ at the end make sure it's an exact match. There are two $'s before 'array', because $array isn't a real array at all, it's actually something which merely refers to the @array, so in order to actually retrieve any values from it, it has to be dereferenced, and you can do this by prepending a $. (You can also do $array->[$i] I think).

              Hope that helps a bit.

              Comment


              • #8
                Well, english isn't my primairy language, but I'll just read over it a couple of times and hope that's make me understand it.

                Thanks a lot for all help
                My Website
                010100010011110101110100011011110111000001101000

                Comment


                • #9
                  Hmm yeah, my explanation wasn't great....

                  It might not be much easier in terms of reading - but this will be more complete than anything I produce, and could help clear things up for you. I took a while to get the hang of references myself..

                  Comment


                  • #10
                    That URL made most of it clear

                    It even made me able to solve an other problem I gave up on

                    You've been (great)

                    Thanks
                    My Website
                    010100010011110101110100011011110111000001101000

                    Comment

                    Working...
                    X