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!

Count and Forward

Status
Not open for further replies.

mxds

Programmer
Jan 27, 2005
9
CH
Greetings everyone!

I'm a newbie to CGI, hopefully some of you more experienced programmers can help me out.

I have a CGI script which i only want to allow beeing executed 1000 times a day. after the script is beeing called more than 1'000 times i want to redirect the users to the page "sorry.htm"

In php this would be easy. i don't know in cgi...

i thought about adding a simple count function which i found here..

Code:
open (COUNT, "<../cgi-bin/count.txt") ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
my $counter = <COUNT>;
close (COUNT);
++$counter;
open (COUNT, ">../cgi-bin/count.txt") ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
print COUNT "$counter";
close (COUNT);

as far as i understand i have the page views in variable $counter now.

now i need to redirect if more than x views, else i want to execute the script...

Code:
if ($counter > 1000 ) {
     print "<meta http-equiv=\"refresh\"
     content=\"0:url=http:/foo.com/sorry.htm\">";
   }

can this be done like that? any tip or snipplets? i searched the whole night but wasn't able to get it running...

maybay someone will point me in the right direction...

cheers mxds
 

by the way... the counter thing works fine.. the redirection doesn't
 
sorry for noobing around... i have a working code by now..

this is what i've done... maybe somebody can confirm whether this is a proper way to do it...

Code:
########################################
# Count and forward
########################################
open (COUNT, "<count.txt") ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
my $counter = <COUNT>;
close (COUNT);
++$counter;
open (COUNT, ">count.txt") ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
print COUNT "$counter";
close (COUNT);

if ($counter > 1000 ) {
	print "Location: [URL unfurl="true"]http://youfool.com/sorry.htm\n\n";[/URL]
   }
 
Why do you need to open and close a file 1000 times just to write a value to it.

Code:
#!/usr/bin/perl

use CGI;
$q=new CGI;
  for ($loop=0; $loop++; $loop<1000 ) {
#do whatever you need to here
  }
  print $q->redirect('[URL unfurl="true"]http://somewhere.else/sorry.htm');[/URL]

HTH
--Paul

cigless ...
 
Hi and thanks for your reply...

Your code is, i guess, an internal array which loops 1000 times before sending the user to '
i have a traffic problem and would like to limit the total calls of this script to, let's say, 1000

if the script was called more than 1000 times from all of the users during a day, i would like to redirect the user to a 'sorry traffic limited exceeded for today'-page ...

did i interpret your code wrong?
 
Code:
#!/usr/bin/perl

use CGI;
  $q=new CGI;
  open FH, "<counter.txt";
  $count=<FH>;
  close FH;
  $count++;
  if ($count > $threshold) {
     print $q->redirect('[URL unfurl="true"]http://somewhere.else/sorry.htm');[/URL]
  } else {
     open FH, ">counter.txt";
     print FH "$count";
     close FH;
  }
you're flocking the file, but not unflocking it. I'd have thought you'd only need to flock the file for writing
Code:
attempt:
  attempt to lock file
  locked ?
     proceed
  else
     attempt
proceed:
also you can't be guaranteed a lock, because something else might have it locked, and you'll have to wait for release

HTH
--Paul

cigless ...
 
Code:
########################################
# Count and forward
########################################
my @date_array = localtime ;
my $year = sprintf("%02d",$date_array[5]-100);
my $month = sprintf("%02d",$date_array[4] + 1) ;
my $day = sprintf("%02d",$date_array[3]);

open (COUNT, "<log/$day$month$year.txt");
my $counter = <COUNT>;
close (COUNT);
$counter++;

if ($counter > 1000 ) {
	print "Location: [URL unfurl="true"]http://somewhere.else/index.htm\n\n";[/URL]
   } else {
   	open (COUNT, ">log/$day$month$year.txt");
	print COUNT "$counter";
	close (COUNT);	
}

thanks again. does this look better now?
 
I prefer to use the CGI module
print $q->redirect

But if it works, it works, you might need to look into the flocking issue though

--Paul

cigless ...
 
thanks again... i will :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top