Solved

perl web counter

Posted on 1998-10-07
19
321 Views
Last Modified: 2008-02-26
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....
0
Comment
Question by:activegs
  • 8
  • 5
  • 4
  • +2
19 Comments
 
LVL 3

Expert Comment

by:zhongbing
ID: 1205187
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205188
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.
0
 

Author Comment

by:activegs
ID: 1205189
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
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:activegs
ID: 1205190
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

0
 

Expert Comment

by:mazares
ID: 1205191
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.

0
 

Expert Comment

by:mazares
ID: 1205192
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.
0
 

Author Comment

by:activegs
ID: 1205193
<!--#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?
0
 

Expert Comment

by:mazares
ID: 1205194
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.
0
 

Author Comment

by:activegs
ID: 1205195
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?

0
 
LVL 4

Expert Comment

by:martinag
ID: 1205196
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
0
 

Author Comment

by:activegs
ID: 1205197
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?
0
 
LVL 4

Expert Comment

by:martinag
ID: 1205198
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
0
 

Author Comment

by:activegs
ID: 1205199
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
0
 
LVL 4

Expert Comment

by:martinag
ID: 1205200
Who should answer?

Martin
0
 

Author Comment

by:activegs
ID: 1205201
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
0
 

Expert Comment

by:mazares
ID: 1205202
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.
0
 
LVL 4

Accepted Solution

by:
martinag earned 50 total points
ID: 1205203
2.

Martin
0
 

Author Comment

by:activegs
ID: 1205204
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.:)
0
 
LVL 4

Expert Comment

by:martinag
ID: 1205205
Thanks. It took me an hour to find the calculator and two hours to understand the instructions ;-)

Martin
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now