Gossamer Forum
Home : General : Perl Programming :

download counter not accurate on mac

Quote Reply
download counter not accurate on mac
I have recently downloaded a download counter from focal media.

I have installed onto a unix server and tested in both mac and pc environments.

Works great on pc, but if I download a file from a mac, each download counts as 2.

Code below. Any ideas appreciated Smile

Code:


if ($ENV{'QUERY_STRING'} ne "") {

$temp = $ENV{'QUERY_STRING'};

}

else

{

read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});

}





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



foreach $item(@pairs)

{

($key,$content)=split (/=/,$item,2);

$content=~tr/+/ /;

$content=~ s/%(..)/pack("c",hex($1))/ge;

$fields{$key}=$content;

}



$fields{'comment'}=~s/\cM//g;

$fields{'comment'}=~s/\n\n/<p>/g;

$fields{'comment'}=~s/\n/<br>/g;



$fields{'download'} =~ s/\;//g;

$fields{'download'} =~ s/^\s+//g;

$fields{'download'} =~ s/\s+$//g;



############################################################

############################################################

############################################################

############################################################



### ERROR CHECKING



$file_exists = (-e "$download_loc/$fields{'download'}");



if ($file_exists < 1)

{

&file_not_exist;

exit;

}







### OPERATIONS

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



$exists1 = (-e "$data_dir/index.idx");

if ($exists1 > 0)

{

open (DNL, "$data_dir/index.idx");

while (defined($line=<DNL>))

{

####### filename, Number of Downloads

($filename, $counter, $tmp) = split (/:-:/,$line, 3);



#print "$filename __ $counter __ $tmp <br>";



if ($filename eq "$fields{'download'}")

{

$counter++;

$towrite = $towrite . $filename . ":-:" . $counter . ":-:" . "\n";

$written = "true";

}

else

{

$towrite = $towrite . "$line"; #. "\n";

}



}

close (DNL);



if ($written ne "true")

{

open (DNL, ">> $data_dir/index.idx");

print DNL $fields{'download'} . ":-:" . "1" . ":-:" . "\n";

close (DNL);

}

else

{

open (DNL, "> $data_dir/index.idx");

print DNL $towrite;

close (DNL);

}

}

else

{

$counter = 1;



if ($written ne "true")

{

open (DNL, "> $data_dir/index.idx");

print DNL $fields{'download'} . ":-:" . "1" . ":-:" . "\n";

close (DNL);

}



}





##### RECORD INFO

$rfile = $fields{'download'};



$fields{'download'} =~ tr/./_/;

$fields{'download'} = $fields{'download'} . ".txt";



$ip_address = $ENV{'REMOTE_ADDR'};

$browser = $ENV{'HTTP_USER_AGENT'};



($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime();

$mon++;

$year = 1900 + $year;

$today = $year . "-" . $mon . "-" . $mday . " " . $hour . ":" . $min . ":" . $sec;





open (DLD, ">> $data_dir/$fields{'download'}");

######### WEBURL HEAR OF NAME

print DLD $fields{'weburl'} . ":-:" . $fields{'where'} . ":-:" . $fields{'name'} . ":-:" .

#EMAIL MAILING LIST IP

$fields{'email'} . ":-:" . $fields{'ml'} . ":-:" . $ip_address . ":-:" . $browser . ":-:" .

#TODAY

$today . ":-:\n";

close (DLD);







print "Location: $download_files/$rfile\n\n";

exit;













sub file_not_exist

{



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



print <<End_of_nexist;



<html>



<head>

<title>File Not Found</title>

</head>



<body>



<p>&nbsp;</p>



<p>&nbsp;</p>



<p>&nbsp;</p>

<div align="center"><center>



<table border="0" cellspacing="0" cellpadding="0" width="400">

<tr>

<td bgcolor="#000000"><table border="0" cellspacing="1" width="100%" cellpadding="6">

<tr>

<td width="100%" bgcolor="#FFCC00" valign="top"><p align="center"><font face="Verdana"

size="2"><b>404 File Not Found</b></font></td>

</tr>

<tr>

<td width="100%" bgcolor="#FFFFFF" valign="top"><font face="Verdana" size="2"><b></b>

&nbsp;&nbsp;&nbsp; <br>

The file you requested could not be found. Please use the back button of your browser to

go back.<br>

&nbsp;&nbsp;&nbsp; <b></b></font></td>

</tr>

</table>

</td>

</tr>

</table>

</center></div>

</body>

</html>



End_of_nexist



}
Quote Reply
Re: [fluffyduck] download counter not accurate on mac In reply to
I would be more inclined to use something like this;

Code:
#!/usr/bin/perl

use CGI;
my $IN = new CGI;

my $file = $IN->param('download');

my $read_write = $file;
$read_write =~ s/\//_/gi;
$read_write =~ s/ /_/gi;
$read_write =~ s/:/_/gi;
$read_write =~ s/http:\/\///gi;


# if this hasn't been counter before, then create a new file...
if (!-e $read_write) {

open(READFILES,">$read_write") || die $!;
print READFILES '1';
close(READFILES);

# redirect to the file...
print "Location: $file \n\n";
exit;

} else {

my $hits;
open(READFILES,"<$read_write") || die $!;
$hits = <READFILES>;
close(READFILES);

$hits++;

open(READFILES,">counter.cnt") || die $!;
print READFILES $hits;
close(READFILES);

# redirect to the file...
print "Location: $file \n\n";
exit;

}

Its untested.. but should work Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] download counter not accurate on mac In reply to
Hmm that has quite a few lines of unneeded code, for example the following:

Code:
$read_write =~ s/\//_/gi;
$read_write =~ s/ /_/gi;
$read_write =~ s/:/_/gi;

...could be written as:

Code:
$read_write =~ y$ /:$_$/;

You can't have a difference in case in that specific regex either so the "i" is doing nothing.

Last edited by:

Recall: Dec 2, 2003, 9:29 AM
Quote Reply
Re: [Recall] download counter not accurate on mac In reply to
True. Even this would work;

$read_write =~ s,(/| |:),_,g;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] download counter not accurate on mac In reply to
tr/// or y/// gives better performance than a substitution, also using () captures the match as assigns it to $1 but you don't require it for anything so you should use either:

Code:
$read_write =~ s,(?:/| |:),_,g;

Or better would be to use a character class...

Code:
$read_write =~ s,[ /:],_,g;
Quote Reply
Re: [Recall] download counter not accurate on mac In reply to
Smart arse ;-)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] download counter not accurate on mac In reply to
Just trying to help.
Quote Reply
Re: [Recall] download counter not accurate on mac In reply to
Thank you very much to both of you for your help.

Problem is i'm a bit disabled in Perl Crazy

I was hoping for someone to basically say 'Yeh, just change that line of code to this...' Smile

If any one knows how to modify the existing code, I would appreciate it. This code is being used along side another and I'm a bit hesitant to change any variable names.

Thanks again for your help. Cool
Quote Reply
Re: [fluffyduck] download counter not accurate on mac In reply to
It's very difficult to diagnose a problem with just a large block of code and no ability to debug.

However I would say that code is very bloated and not particularly well written and I'd consider changing to something else if I were you.

I've attached a brief sample of a counter. Maybe you can apply it for your needs.

Last edited by:

Recall: Dec 3, 2003, 5:24 PM