CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
counter(s)

 



tucats
Deleted

Jan 19, 2000, 3:14 AM

Post #1 of 5 (1249 views)
counter(s) Can't Post

I have a project that I am working on, and I am not very well versed in perl...
what I need from perl is a counter that will work for "thousands" of web pages and sites that are setup as subdomains... i.e. http://www.perlguru.com/????.htm... I heard perl was the place to look, and I am not knowledgeable in perl, so I don't know how to set up a counter program that will handle all of these websites "separately"...

If this question is easy... I apologize in the beginning...

But I would appreciate any input that you might have...

thank you...


Jasmine
Administrator / Moderator

Jan 19, 2000, 4:20 AM

Post #2 of 5 (1249 views)
Re: counter(s) [In reply to] Can't Post

You may wish to check out Run Your Own Counter Service. The site says:

"Imagine providing your users with their own counters! You now have that option. This script is fully customizable with headers, footer, fully customizable colors, and customizable images! You can have an unlimited amount of counter digits to choose from! All this will guarentee visitors to your site"

If this isn't what you need, please let us know what's missing.

Good luck!


tucats
Deleted

Jan 23, 2000, 3:15 PM

Post #3 of 5 (1249 views)
Re: counter(s) [In reply to] Can't Post

Actually, I am going to be running a site that will offer free websites, hosted on my domain... I need a script that will allow me to put a counter on all of the sites...

I have a script now that is not working, maybe you can take a look and let me know what I am doing wrong...


#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

open(COUNTER, "< $fields{'counter_file}") &#0124; &#0124; die "BUSY";
$value = <COUNTER>
close(COUNTER);

$value++;
open(COUNTER, "> $fields{'counter_file}") &#0124; &#0124; die "BUSY";
print COUNTER $value;
close(COUNTER);
#
print $value;

exit(0);

Do I need to add:

sub parseInput()
{

if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
}

elsif ($ENV{'REQUEST_METHOD'} eq "GET" )
{
$temp = $ENV{'QUERY_STRING'};
}

@pairs=split(/&/,$temp);

foreach $item(@pairs)
{
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}

}


.........??????? I was told I would need that latter part, but don't see the use in it for a simple counter... but then again, I am a beginner...

and how do I capture the address that the counter query is coming from and write it to "counter.dat"...?

then in the .htm page I just add:


<!--#exec cgi="counter.cgi?counter_file=xxxxxx" -->

I have tried all kinds of things and can't get this script to work...

also, I would like to write all of the data to one file... "counter.dat"

Please help...



Jasmine
Administrator / Moderator

Jan 23, 2000, 7:34 PM

Post #4 of 5 (1249 views)
Re: counter(s) [In reply to] Can't Post

Whoah... gaping security hole !

But first, yes, if you want to pass information to the program like this, <exec cgi="counter.cgi?counter_file=xxxxxx>, you need to tell the program what's the variable name and value. That's what your parseinput subroutines does.

So put the sub parseInput subroutine in (and get rid of the () that's after the sub parseInput that you have in your code), and put

&parseInput;

before you start opening files -- this invokes the subroutine, which will assign the information you're passing to the program, in this case, $fields{'counter_file'}

As for the security hole... you never want to allow anyone to pass a whole filename for opening and manipulating on your server.

What if someone passed <exec cgi="counter.cgi?counter_file=/home/yourdomain/www/index.html> (or worse -- a password file)? The counter program would open it, increment it, and display it in the browser. Voila! A messed up home page.

The best thing to do is set a complete server path for the file, and if the file has a static suffix, toss that in there too for good measure (and don't pass the file suffix in your exec cgi call, just the filename). Example:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

&parseInput;

$serverpath = "/server/path/to/counter/file";
$file = "$serverpath/$fields{'counter_file'}.dat";

open(COUNTER, "< $file") &#0124; &#0124; die "Couldn't open $file for reading $!";
$value = <COUNTER>;
close(COUNTER);

$value++;
open(COUNTER, "> $file") | | die "Couldn't open $file for writing $!";
print COUNTER $value;
close(COUNTER);
#
print $value;

parseInput sub {
# code
}
</pre><HR></BLOCKQUOTE>



tucats
Deleted

Jan 23, 2000, 7:50 PM

Post #5 of 5 (1249 views)
Re: counter(s) [In reply to] Can't Post

Jasmine...

I threw all of that away, now I have a counter program that will count endless number of pages and print the results on one page...

#!/usr/local/bin/perl


$counter_file = "counter.dat";

$pagepath = $ENV{'DOCUMENT_URI'};

print "Content-type: text/html\n\n";

open(FILE, ">>$counter_file");
close(FILE);

open(FILE, "$counter_file");
@indata = <FILE>;
close(FILE);

$onoff = 0;
open(FILE, ">$counter_file");
foreach $temp (@indata)
{
chop($temp);
($uri, $count) = split(/\|/, $temp);
if ($uri eq $pagepath) {
$count++;
$onoff = 1;
print FILE "$uri|$count\n";
print "$count"; }
else { print FILE "$uri|$count\n"; }
}
if ($onoff eq 0) {
print FILE "$pagepath|1\n";
print "1"; }

close(FILE);


what i need is instead of printing all of the results to one page... "counter.dat", i would like it to create separate pages for each URI... how could i implement this...?

thank you for your help...


[This message has been edited by tucats (edited 01-24-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives