Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit hits per IP

Status
Not open for further replies.

mxds

Programmer
Jan 27, 2005
9
CH
Hello.

I need to be able to "limit simple page hits per IP" for a perl script on my site. I have searched the web for examples or scripts but couldn't find anything.

Does anybody know of a perl snipplet doing something like this?

Thanks in advance for any tips that could point me into the right direction...

Stefan
 
I already have some code. I call the script using

Code:
require "check_limit.pl";
if ($clicks_this_ip > 100 ) {
     deny access..

the check_limit.pl looks like this... a modified users online script....

Code:
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

#############################################################
# Set program parameters here                               #
#############################################################

# List of IP addresses to ignore, add your own IP to the list
@ignore = ("127.0.0.1");

# Time (in minutes) to show the user as online after the last request
$limit_period = 60;

# Time (in minutes) to save the user's IP after the last request. The counter is only increased for unknown IPs.
$keep_in_log_time = 60;

# Set to 0 if your server's operating system doesn't implement flock() (for example Windows 95/98/ME)
$use_flock = 1;

# Directory of the counter templates (counter_xxx.html)
$templ_dir = ".";

# Directory of the counter data (log.txt and count.txt)
$data_dir = "Data";

#############################################################
# Program starts here                                       #
# GTCounter 1.1 by Wladimir Palant ([URL unfurl="true"]http://www.gtchat.de/)[/URL]  #
#############################################################

$LOCK_EX = 2;
$LOCK_UN = 8;
$current_time = time;

open(LOG, "+>>$data_dir/log_m.txt");
flock(LOG,$LOCK_EX) if ($use_flock);
seek(LOG,0,0);

@entries = <LOG>;

seek(LOG,0,0);
truncate(LOG,0);

$add=1;
$clicks_this_ip=1;
$ignore=0;
foreach (@ignore)
{
	if ($ENV{REMOTE_ADDR} eq $_)
	{
		$add=0;
		$online=0;
		$ignore=1;
	}
}

print LOG "$ENV{REMOTE_ADDR}|$current_time\n" if (!$ignore);
foreach $curentry (@entries)
{
	# Steuercodes löschen
	$curentry =~ s/[\n\r]//g;


	($ip, $time) = split(/\|/, $curentry);


	# Nur solche behalten, welche z.B. innerhalb einer Stunde sind
	if ($current_time - $time <= $keep_in_log_time*60)
	{
		print LOG "$curentry\n";
	}
	if ($current_time - $time <= $limit_period*60 && $ip eq $ENV{REMOTE_ADDR})
	{
		$clicks_this_ip++;
	}

	

}
flock(LOG,$LOCK_UN) if ($use_flock);
close(LOG);



if (!defined($no_output))
{
	print "Content-type: text/html\n";
	print "Pragma: no-cache\n";
	print "Expires: now\n\n";
	
	$ENV{QUERY_STRING} =~ s/\W//g;
	if ($ENV{QUERY_STRING} eq "")
	{
		open(FILE,"$templ_dir/counter.html");
	}
	else
	{
		open(FILE,"$templ_dir/counter_$ENV{QUERY_STRING}.html");
	}
	
	foreach (<FILE>)
	{
		$_ =~ s/{ONLINE}/$clicks_this_ip/g;
		$_ =~ s/{COUNT}/$count/g;
		print "$_";
	}
	close(FILE);
	exit;
}

1;

suprisingly this seems to work... what do the experts think about?
 
If it works, and you've tested it then, it's good to go ;-)
--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I don't see how it can work to limit page hits. I have a feeling it seems to be working but maybe not for the reasons you think.

If you wanted to limit page hits you should be comparing (I think) $clicks_this_ip to a value (a maximum limit I assume), but thats not being done anywhere in the script.
 
Hello and thanks for responding...

Yes I do compare $clicks_this_ip to a value.

Code:
require "check_limit.pl";
if ($clicks_this_ip > 100 ) {
     deny access..
 
ahhh! sorry, I missed that. I'll put on my glasses next time. [glasses]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top