where is my text file?

C

code_wrong

hi,
I using the following script (from CGI and Perl in easy steps book) as a hit
counter:
it works ... but when I view the files on the host with WSFTP there is no
sign of counter.txt...
the script is in cgi-bin .. which is surely where counter.txt should appear
... but there is no sign of it anywhere .. any ideas?

#!/usr/bin/perl

open(COUNT,"counter.txt");
$num = <COUNT>;
close(COUNT);

$num++;

open(COUNT, ">counter.txt");

print COUNT $num;
close(COUNT);

$num=sprintf("%05d", $num);

print "content-type:text/html\n\n <html>";
print "You are visitor number $num </html>";
 
C

code_wrong

code_wrong said:
hi,
I using the following script (from CGI and Perl in easy steps book) as a
hit counter:
it works ... but when I view the files on the host with WSFTP there is no
sign of counter.txt...
the script is in cgi-bin .. which is surely where counter.txt should
appear .. but there is no sign of it anywhere .. any ideas?


DOH! .. found it ... I couldn't see it for looking at it ;-) it was in my
root directory on the host
 
D

Dr.Ruud

code_wrong:
I using the following script (from CGI and Perl in easy steps book)
as a hit counter:

Try <
Appending a 0-byte per visitor, to a sparse file if available, may be a
better approach.
 
J

J. Gleixner

code_wrong said:
DOH! .. found it ... I couldn't see it for looking at it ;-) it was in my
root directory on the host

Wait a second.. The user-id running your Web server has write privilege
to your root directory? That's not good.

As for a counter, please see:

perldoc -q "I just want to increment the number in the file."
 
C

code_wrong

Purl Gurl said:
code_wrong wrote:

(snipped)



You don't want to do that. If your server is configured
correctly, visitors cannot directly access your cgi-bin
directory. Executables in a cgi-bin directory are
used for server side includes or adjunct programming
for other scripts, located outside your cgi-bin directory.

my cgi-bin directory is located in my home directory along with my
index.shtml file and scripts can be run by inserting a direct url to a
script file ...... This is not good, but it can work as long as say a script
requires certain parametres to run ..for example some form data .. is this
workable?
Your content type is provided by default for html pages
or is printed within a cgi script, as needed.

I don't understand this ..the content type is printed by my script .. but it
does not show up in the source when I view source .. why is this? .. Bear in
mind the script I am using has come from a book ...
<!--#include virtual="cgi-bin/counter.pl"-->

this is just another way to invoke the script via SSI

cheeers
cw
 
T

Tintin

code_wrong said:
hi,
I using the following script (from CGI and Perl in easy steps book) as a
hit counter:

Throw that book away. The code example is simply woeful. You will be
learning many, many bad habits if you follow the book.

[horrible code snipped]
 
C

code_wrong

Tintin said:
code_wrong said:
hi,
I using the following script (from CGI and Perl in easy steps book) as a
hit counter:

Throw that book away. The code example is simply woeful. You will be
learning many, many bad habits if you follow the book.

[horrible code snipped]

how so?

it does go on to demonstrate file locking and permissions ... although ..
since my host is running Windows IIS .... I am not sure if I can set file
permissions
 
E

Eric Bohlman

Tintin said:
Throw that book away. The code example is simply woeful. You will
be learning many, many bad habits if you follow the book.

[horrible code snipped]

how so?

it does go on to demonstrate file locking and permissions ... although
.. since my host is running Windows IIS .... I am not sure if I can
set file permissions
#!/usr/bin/perl

No "use warnings" (or -w).
No "use strict".
open(COUNT,"counter.txt");

No check that the open succeeded.
Assumption that the file is in the script's current working directory;
where that is is extremely server-dependent.
$num = <COUNT>;

$num++;

Up to this point, another instance of this script could also have read
the same value from the file.
open(COUNT, ">counter.txt");

No check that the open succeeded.

At this point, the file is empty. If another instance of the script
comes along and tries to read it, its version of $num will be set to
zero.
print COUNT $num;
close(COUNT);

In the first scenario I mentioned, the count will have only gone up by
one even though there were two accesses because both instances started
with the same value. In the second scenario, the count will be reset to
1 because the second instance read from an empty file.
$num=sprintf("%05d", $num);
print "content-type:text/html\n\n <html>";

You've advertised that you're sending out HTML.
print "You are visitor number $num </html>";

But what you've sent out isn't HTML (there's no <title>).
 
C

code_wrong

Eric Bohlman said:
Tintin said:
Throw that book away. The code example is simply woeful. You will
be learning many, many bad habits if you follow the book.

[horrible code snipped]

how so?

it does go on to demonstrate file locking and permissions ... although
.. since my host is running Windows IIS .... I am not sure if I can
set file permissions
#!/usr/bin/perl

No "use warnings" (or -w).
No "use strict".
open(COUNT,"counter.txt");

No check that the open succeeded.
Assumption that the file is in the script's current working directory;
where that is is extremely server-dependent.
$num = <COUNT>;

$num++;

Up to this point, another instance of this script could also have read
the same value from the file.
open(COUNT, ">counter.txt");

No check that the open succeeded.

At this point, the file is empty. If another instance of the script
comes along and tries to read it, its version of $num will be set to
zero.
print COUNT $num;
close(COUNT);

In the first scenario I mentioned, the count will have only gone up by
one even though there were two accesses because both instances started
with the same value. In the second scenario, the count will be reset to
1 because the second instance read from an empty file.
$num=sprintf("%05d", $num);
print "content-type:text/html\n\n <html>";

You've advertised that you're sending out HTML.
print "You are visitor number $num </html>";

But what you've sent out isn't HTML (there's no <title>).

thanks this is useful stuff ....
I need to add error checking for file open
File locking for reading and writing
I know where the file is because I am choosing the path now:
open(COUNT,"<..\\private\\counter.txt");
and I need to print valid HTML
'use warnings' and 'use strict' I need to look into
cheers
 
A

A. Sinan Unur

I know where the file is because I am choosing the path now:
open(COUNT,"<..\\private\\counter.txt");

Wrong again on at least two counts:

1. The path you specify is still relative. There is no guarantee that
the location to which it is relative will always be the same. You should
specify an absolute path.

2. You still do not check if open succeeded.

3. Using lexical filehandles and the 3-argument form of open is
preferable these days.

So, putting those together:


use strict;
use warnings;

use Readonly;
Readonly my $COUNTER_FILE => 'D:/wwwroot/private/counter.txt';

open my $count_fh, '<', $COUNTER_FILE
or die "Cannot open $COUNTER_FILE: $!";

# etc
'use warnings' and 'use strict' I need to look into

They are not optional, especially at your level. Please read the posting
guidelines.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,497
Latest member
PilarLumpk

Latest Threads

Top