easy!!hit counter

Can someone write me a quick cgi script that will open a file off the net, read the data in it (it will be numerical) add 1 to the data then write to the file again with the new value in it...just like a hit counter.

thanks you
wormboy__6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jhurstCommented:
If you really mean off the net.  Not possible.  If you mean on your own system,

#!/usr/bin/perl
open(FILE,"counter.txt");
$oldCount=<FILE>;
close(FILE);
open(FILE,">counter.txt");
$newCount=$oldCount+1;
print FILE "$newCount\n";
close(FILE);

#the section that follows will allow you to call it like a counter:
print <<EOT;
Content-type: text/html

<html>
<body>
Counter value is: $oldCount
</body>
</html>
EOT
exit;



Now, in your pages you can do:
<img src=http://yourscript width=1 heigth=1>
You will never see it and the counter will be incremented.  To look at the value, you just then
http://yourscript
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wormboy__6Author Commented:
Im sorry, i should have explained that i want it to reset every day. ie go back to zero.

Could you please add in the code to do this - or even better - i select a time that it will zero itself at.

thanks
0
jhurstCommented:
There is going to be a problem with resetting it at the same time every day since you can not be certain that the script runs at that time, as it will only be invoked when you call it, and this may not be the required time.  But the following addidion before the open(FILE,">counter.txt"); will probably do what you want.

open(FILE,"<dayreset.txt");
$lastReset=<FILE>; #get the day reset last time
close(FILE);
$timenow=localtime;
@f=split($today); #separate the day of week, month, day of month, etc
$x=$f[1] . $f[2]; #the month and day of month
if ($f[3] > "12:00" && $x ne substr($lastReset,0,5))
  {# the time to reset is passed, being preset at 12:00 (noon) and
    # this is not the day we reset
   $oldCount=0; #so - reset the counter
   open(FILE,">dayrset.txt");
   print FILE "$x\n"; #remember that we reset today so we don't again
   close(FILE);
   }

   
I suspect that you do not yet know much perl, therefore let me point out that you will need to create the two files, counter.txt and dayreset.txt otherwise the script will fail since it will not find the files.  You can even automate this if you want by placing the following code at the very start of the script:

if (!( -r counter.txt)) {open(FILE,">counter.txt");close(FILE)} #creat if none
if (!( -r resetday.txt)) {open(FILE,">resetday.txt");close(FILE)} #creat if none
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ozoCommented:
if you want to be sure that two hits at the same time won't corrupt the counter, you should flock the critical region
0
jhurstCommented:
I agree with ozo but realisticall the risk is only that one count will be lost and the performance hit is considerable, and the risk very small.  But he is correct.
0
ozoCommented:
Depending on the timing, all counts could be lost.
But that may be an acceptable risk for your purposes.

It might be easier to get the last reset time with -M or stat
0
wormboy__6Author Commented:
hmmm could u write this into a script for me...as i dont know any perl sorry..im just learning
0
jhurstCommented:
ozo, How could all counts be lost?  I seem to be missing something.  Or are you thinking that the first job has the output open but not yet written and the second read it, see's nothing and then updates this zero value.  If yes, I sort of agree and may be it would be better to open the files in update mode rather than write mode before re-writing the values.  This would be better and removes that VERY SLIGHT risk.  Then just change my open(FILE,"> to open(FILE,"+

wormboy, what exactly do you need that is not here?
0
wormboy__6Author Commented:
i just meant about the -M or stat..but dont worry, ill try changing the file to +.

also, in resetday.txt, what format do i put the time to reset in?

0
jhurstCommented:
does not matter what you put in the resetday.txt file it will be reset automatically to the format of time on your system.  You merely need to have the file there.

On the -M I do not understand the question but you say that it does not matter.
0
wormboy__6Author Commented:
reset on the time on your system...do you mean the clients system or the system where the cgi is?

Because if its the client system, it will not work as everyone has a different time setting round the world.
0
wormboy__6Author Commented:
Also, can you please write me the exact code that i can save as a web page on the net and when people go to it, the cgi is executed (i dont know anything about cgi or html hardly)
0
jhurstCommented:
are you sure that your hosting service even supports cgis written by users in perl?
0
ozoCommented:
#!/usr/bin/perl
use Fcntl ':flock'; # import LOCK_* constants
open(FILE,"<counter.txt");
flock(FILE,LOCK_EX);
$resettime = 12.0;
($h,$m) =  (localtime)[2,1];
$h += $m/60;
$resettime -= 24 if( $h < $resettime );
$Count=<FILE> if( -M FILE < ($h-$resettime)/24 );
$Count++;
open(NEW,">counter.txt");
print NEW "$Count\n";
close(NEW);
close(FILE);
0
wormboy__6Author Commented:
yes i can have cgi scripts on the site that is hosting me...now how do i add this to a page....please tell me the exact code to place...

thanks
0
ozoCommented:
What do you mean by add this to a page?
As jhurst said, you can call it from your page with
<img src=http://yourscript width=1 heigth=1>
0
wormboy__6Author Commented:
hmm ive tried that but it doesnt work.

Im hosted with virtual ave.
Can you normally put the url of the script into a browser and go straight to it...?

I cant.
It says internal server error i think.

Am i doing anything wrong, or is it the server?
0
jhurstCommented:
yes, you should be able to put that url straight there - sounds like you have not set the script up correctly to be executable, chmod 755!
0
wormboy__6Author Commented:
chmod 755??

whats this?
0
wormboy__6Author Commented:
ok..ive changed the permissions but i still get the internal system error.

Do you know why? i am using virtualave.com to provide me.

0
wormboy__6Author Commented:
ozo..your code doesnt work either...It creates a file with "1" in it..but then when the script is run again...it doesnt increase the value of the file.

Can you please change your code...thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.