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: Intermediate:
File corrutions

 



benn600
User


Oct 27, 2004, 4:09 PM

Post #1 of 5 (527 views)
File corrutions Can't Post

I wrote a review for the HP iPAQ HX4700 located at http://benn600.no-ip.org/public/hx4700/review.html and have a counter set up for it in cgi-perl located at http://benn600.no-ip.org/cgi-bin/hx4700counter/master.pl but when you click on the link at the bottom called "View Advertisement Pages Only", you see the data file just spilled out, excluding a few items and look at how some of the lines are corrupt! This keeps happening and I think it has something to do with me using a windows server and therefore, cannot use flock! I tried it once a few days ago but it deleted all the info from the data file and the last backup I did was over a day ago, so I lost about 36 hours of data, which didn't make me happy at all.

What can I do to fix this problem? I am desperate to solve it because this makes cgi-bin virtually useless for applications in which I can make money or receive heavy traffic when it keeps corrupting the "database" (it is only a .txt file). If you have any questions or can answer my question, please post them here...thank you VERY much!
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


KevinR
Veteran


Oct 27, 2004, 11:13 PM

Post #2 of 5 (525 views)
Re: [benn600] File corrutions [In reply to] Can't Post

post the code for the counter.

I tried to look at the page, but your server is either tremendously overworked or extremely slow, I gave up after a few mintues of trying to have the page finish loading. What I saw did not look garbled (or corrupted, whatever that means), although the html could maybe be improved as the table cells that were empty were causing a small display problem (Mozilla).
-------------------------------------------------


benn600
User


Oct 28, 2004, 4:08 AM

Post #3 of 5 (521 views)
Re: [KevinR] File corrutions [In reply to] Can't Post

First of all, thank you very much for your quick response. Here is the subroutine that writes to the counter file:



if ($action eq "counter") {
if ($display eq "yes") {&display;}

if ($ENV{'REMOTE_ADDR'} ne "192.168.2.2" && $ENV{'REMOTE_ADDR'} ne "192.168.2.3" && $ENV{'REMOTE_ADDR'} ne "192.168.2.4" && $ENV{'REMOTE_ADDR'} ne "192.168.2.5" && $ENV{'REMOTE_ADDR'} ne "192.168.2.6" || $specialtemp eq "off") {

@newall = ("$host");
open(COUNTER,"data/counter.txt") or dienice(print "Can't open counter.txt: $!\n");
@counter = <COUNTER>;close(COUNTER);
foreach $line(@counter) {chomp($line);
($page,$ip,$visits,$dates) = split(/\|/,$line);

if ($page eq "$host") {if ($ip eq $ENV{'REMOTE_ADDR'}) {
open(COUNTER,"data/counter.txt") or dienice(print "can't open counter.txt $!\n");
@counter = <COUNTER>;close("COUNTER");
open(COUNTER,">data/counter.txt") or dienice(print "Can't open counter.txt for writing: $!\n");
foreach $line(@counter) {chomp($line);($page,$ip,$visits,$dates) = split(/\|/,$line);
if($page eq "$host" & $ip eq $ENV{'REMOTE_ADDR'}) {
if ($dates =~ "$currentdate") {$dates = "$dates, $hr:$min:$sec $ampm";$visits++;}else {
$dates = "$dates\;$currentdate at $hr:$min:$sec $ampm";$visits++;}}

print COUNTER "$page|$ip|$visits|$dates\n";}close("COUNTER"); ### Writing
exit;}}}
open(COUNTER,">>data/counter.txt") or dienice(print "Can't open counter.txt: $!\n");
print COUNTER "$host|$ENV{'REMOTE_ADDR'}|1|$currentdate at $hr:$min:$sec $ampm\n";close("COUNTER");}
}



I think the problems occur when two people visit my site at once OR if they go to it and then click stop on the browser. I fear that when people do this, it deletes whatever part of the file has not been written yet. The file is basically a mb txt file with all counter data. If this worked better and was faster, it would be much better. It is running on a 600mhz basic computer and my dsl line is only 256kb upload. When I access the page from inside my LAN, it is pretty fast. Thank you!
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


KevinR
Veteran


Oct 28, 2004, 11:35 AM

Post #4 of 5 (519 views)
Re: [benn600] File corrutions [In reply to] Can't Post

Yikes! That is some code you have there. Shocked

Dang that is hard to read and understand, and may contain an error or two.

these lines are suspicious:

what is this for?

@newall = ("$host");

this looks bad, not sure if you are trying to match with a regexp or a simple equality operator:

if ($dates =~ "$currentdate")

this looks like a syntax error, use of single & (bitwise and) instead of two && (logical and):

if($page eq "$host" & $ip eq $ENV{'REMOTE_ADDR'}) {

why are you exiting the script here?

exit;}}}

you probably should have a else condition to this if condition otherwise what is the script going to do? It may just do something unexpected since Perl tries to keep executing a script if at all possible:

if ($ENV{'REMOTE_ADDR'} ne '192.168.2.2' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.3' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.4' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.5' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.6' ||
$specialtemp eq 'off') {

[your existing code block]
}

else {
tell the script to exit or return or whatever is appropriate
}


anyways.....

and you open and close the same file handle so many times, it looks unnecessary to me, but maybe not.

I tried to understand your code and made a couple of assumptions and came up with this more streamlined code:



Code
if ($action eq 'counter') { 
if ($display eq 'yes') {&display;}

if ($ENV{'REMOTE_ADDR'} ne '192.168.2.2' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.3' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.4' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.5' &&
$ENV{'REMOTE_ADDR'} ne '192.168.2.6' ||
$specialtemp eq 'off') {

@newall = ("$host");# whats this for?

#open file for reading into array
open(COUNTER,"data/counter.txt") or dienice(print "Can't open counter.txt: $!\n");
@counter = <COUNTER>;
close(COUNTER);

#open file for writing;
open(COUNTER,">data/counter.txt") or dienice(print "Can't open counter.txt for writing: $!\n");
foreach $line (@counter) {
chomp $line;
($page,$ip,$visits,$dates) = split(/\|/,$line);
if ($page eq $host && $ip eq $ENV{'REMOTE_ADDR'}) {
$visits++;
if ($dates eq $currentdate) {
$dates = "$dates, $hr:$min:$sec $ampm";
}
else {
$dates = "$dates;$currentdate at $hr:$min:$sec $ampm";
}
print COUNTER "$page|$ip|$visits|$dates\n";
}
else {
print COUNTER "$line\n";
}
}
close(COUNTER);

open(COUNTER,">>data/counter.txt") or dienice(print "Can't open counter.txt: $!\n");
print COUNTER "$host|$ENV{'REMOTE_ADDR'}|1|$currentdate at $hr:$min:$sec $ampm\n";
close(COUNTER);
}
}


backup your existing file (counter.txt) and give it a try. LIke I said, I am not sure it will do what you want, but look at it and see how it relates to your code and make any changes necessary.
-------------------------------------------------


(This post was edited by KevinR on Oct 28, 2004, 1:49 PM)


ShipOfFools
Novice

Oct 29, 2004, 7:52 PM

Post #5 of 5 (504 views)
Re: [benn600] File corrutions [In reply to] Can't Post

I heard flock is now usable on m$ systems. But anyway, don't try to mix oil and water, use Perl with *nix which is better for networks.

You keep blaming everything on "it". But "it" must have been created by a living organism somewhere. If you can't find the life form that did this, then throw the script out and start again with someone who will look after you.

If you don't want to do that, then show us the offending code. Help us to help you. Know the question to your answer otherwise the answer is 42.

Dave

 
 


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

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