Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool 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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    On the other hand my machine is a 1.2Ghz Athlon... .

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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]

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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]$/

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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..

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

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