PX : code

Counter by Shrihari Saraf
Download this code


<?php
$Datafile 
"count_data.txt";
$DisplayType "images";
$ImgDir "imgcounter/";

read(STDIN$input$ENV{'CONTENT_LENGTH'});

    
$input $ENV{'QUERY_STRING'} if $ENV{'QUERY_STRING'};

        @
pairs split(/&/, $input);

        foreach 
$pair (@pairs) {
        (
$name$value) = split(/=/, $pair);

        
$value =~ tr/+/ /;
        
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C"hex($1))/eg;

    
$INPUT{$name} = $value;
}



$referer $ENV{'HTTP_REFERER'};

$pageuri $ENV{'DOCUMENT_URI'};
$counternm $ENV{'DOCUMENT_NAME'};




#begin count 
open (CNT,"$Datafile") || die "Can't open $Datafile\n";

 

@
CounterFile = <CNT>;
close (CNT);


open (CNTER,">$Datafile") || die "Can't open $Datafile\n";
            
flock (CNTER,2);

    foreach 
$CounterFile (@CounterFile)
        { 
#begin foreach
        
($CounterName,$Otherstuff) = split(/=/,$CounterFile);

        if (
$CounterName eq $pageuri)
            
            { 
#begin second if
            
$count $Otherstuff;
            
chop($count);
            
            
            
$count++;

            
            
$Success 1;
            
            
            print 
CNTER "$pageuri\=$count\n";
            
            
            } 
# end second if
            
else
            {
            print 
CNTER "$CounterFile";
            
next;}
            
    
}
close (CNTER);

&
newname unless $Success;

&
GenerateCounter;

sub newname
{
open(THEFILE,">>$Datafile") || die "Can't open $Datafile\n";
$ToPrint "$pageuri" "\=" "1";
print 
THEFILE "$ToPrint\n";
$count 1;
&
GenerateCounter;
}
        

sub commify
{
    
$text reverse $_[0];
    
$text =~ s/(ddd)(d+)/$1,$2/g;
    
$county reverse $text;
}
sub GenerateCounter
{

if (
$DisplayType eq 'text')
{
    print 
"Content-type: text/html\n\n";
    &
commify($count);
    print 
"$county";
    exit;
}
        

$CountLength length($count);
$numerr 3;
 for (
$i 0$i $CountLength$i++)
    {
        
$A chop($count);
        
$numerr--;
        if (
$numerr == (0-1))
        {
            
unshift(@countlist"comma");
            
$numerr 3;

        }

        
unshift(@countlist$A);
    
    }
  
    print 
"Content-type: text/html\n\n ";

    foreach 
$countlist (@countlist)
    {

        
$CountImage "$ImgDir" "$countlist" ".gif";
        print 
"<img src=$CountImage>";

    }

}
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.