Link to home
Start Free TrialLog in
Avatar of activegs
activegs

asked on

perl web counter

Hi ppl,

just wondering how to run a perl script in the background, say for a web counter.

for instance, if I have index.html is there a way i can open a data file, get the current value, add one to it and close the file with the viewing noticing anything?

like the way counters display an image via a script that does the same, except i dont want to do anything the user can see....
Avatar of zhongbing
zhongbing

you can use you perl cgi as the page. and cat the index.html in the perl file. just like this:

system("cat image.html");

so you can let the image.html and the counter appers in one page.
Avatar of ozo
Assuming your operating system has a `cat` command..

You may also want to lock the file so two users trying to change the current value at the same time don't clash.
Avatar of activegs

ASKER

My system does not have a cat command, I am running NT.

I think the answer lies in HTML syntax wouldn't you think? Not in the perl script itself.

<img src=counter.pl> : this html prompts the browser to get an image/jpg or image/gif generated by a perl script, but the user cannot see the script in action (apart from the generated image). I want the HTML that prompts the browser to run a perl script without having any effect on the user (to simply update a file for my benefit).

ta
Plus also, if i were to use a perl file to write the contents of index.html then if someone typed : www.ActiveGS.com.au/ (my website :))
then index.html would be the default (not index.pl) thus I would lose hit counts.

thnaks again

Here's how I do mine. The html document needs to have a line like this to execute the counter when the document is retrieved:

<!--#exec cgi="/teima/cgi/mycount.cgi"-->

Then, here's the script it executes. As per your request, it opens, read froms, and increments a count number contained in a data file:

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
#============================================================
#                        MYCOUNT.CGI
#============================================================

    $File1 = "+</usr/local/www/data/teima/dat/.mycount";
    $LogRec = "A6";
    open (COUNTFILE, $File1);

    seek(COUNTFILE, 0, 0);
    $buf = read(COUNTFILE, $rec, 6);
    ($num) = unpack($LogRec, $rec);

    $num = $rec + 1;
    seek(COUNTFILE, 0, 0);
    print COUNTFILE pack($LogRec, $num);
    close(COUNTFILE);

    print "<!-- ", $num, " -->\n";

This counter is invisible to the viewer of the html page. You can make it visible by doing something like this for that last line:

print "Yours is visit number", $num, "to our site.\n";

Hope this helps.

Forgot to mention -- you can go see where that script I suggested is used: www.vdospk.com/teima/myweb.htm  Since it's invisible to the user, you have to use your viewer's "view source" function to see the counter number embedded in the code.
<!--#exec cgi="/teima/cgi/mycount.cgi"-->

is an ssi capability isn't it? if i do not have server side includes (my isp doesn't) how can i do this type of thing - or can i?
Geez, activegs, I may not be smart enough to answer that one... I'd say, just try it. I know my isp is about as antiquated as they come, and it works on their machine. Try it and let me know.
it didnt work....however i am wondering if pluging the script into an image that when drawn was 1x1 pixels.

like <img src=perlsc.pl> and it did all the stuff i wanted plus returned a 1x1 image.

could work - might try it.

thanks....anyother suggestions?

You could output the whole HTML from an .pl using content-type: text/html and reading the file in, add the counter at a special location and print the result out...
You want code?

Martin
That is definately an option, but as i think i stated earlier, index.html is the default file to load when no file is specified (meaning that if i depend on index.pl (or whatever) to do the counting, then I will lose hit counts when someone types http://www.ActiveGS.com.au/ rather than http://www.ActiveGS.com.au/index.pl
unless there is a way i can force index.pl to be loaded all the time (on every browser - not just ones that support redirection) then maybe that idea is not real good.

any other suggestions?
Well, most browsers (99.9% or something like that I guess) supports redirecting.

If you want to set index.pl as default, it's rather easy on most servers. I don't know about NT, but such thing should be easy to do.
Either you could just change the setting for the top directory to index.pl, but not for the other. Or you can make index.* default (I think you can use *'s) in all directories.

There is another solution...Have index.html load a frameset with index.pl plus a hidden frame. Then, all links from index.pl loads into the whole browser (TARGET="_top").
But then of course, the browser has to support frames (Again, 99.9% I think).

Martin
I dont know about 99 percent supporting frames, but redirection is common - i have decided to go with the image idea i came up with, it works ok. thanks for all your help, I have no probs handing over the points for all the time you spent. thanks again

email me at John@ActiveGS.com.au for future discussions/help/questions etc.

if you answer the question, i give points

john
Who should answer?

Martin
Uh geez, tough one....all I can really do is ask another question... OK What is 1 + 1 equal to? The first one to answer this gets the points.

:)John
This one really puzzles me... the script I offered earlier, and the "exec cgi" call that executes it, have worked great for me on a lot of different sites. I don't really understand why it wouldn't work for activegs. I trust he changed my literal path and filenames to match those he's actually using.
ASKER CERTIFIED SOLUTION
Avatar of martinag
martinag

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mazares, The script doesnot even execute, some servers do not allow for SSI (server side includes) mine is one of them (Generally NT boxes) exec is an SSI command - it would be the way to go if I did have ssi, but I dont. Thanks again.


By the way, Martinag, brilliant answer.:)
Thanks. It took me an hour to find the calculator and two hours to understand the instructions ;-)

Martin