Hi guys,
 
I am writing a counter that checks where the hit comes from, and if it
comes from the same site, it will not count .
I have a question related to performance:
In the script (as yet) I have to open the txt file twice at this stage,
because I first need to read from it and then write to it. The problem
is that the a+ and w+ property of fopen set the file 0 before I can read
from it. So here is the real question:
Is it possible to do this with only one read? Does it matter performance
wise?
 
The script functions are below:
 
function ref($url)
            {
                        $nc = 0;
                        $cfile = "inc/log/logcount.txt";
                        $valid = preg_match("/dcv/", $url);
                        $fp = fopen($cfile,"r");
                        $pc = fgets($fp,1024);
                        
                        if($valid){
                                    echo $pc;
                        } else {
                                    $this->incr($pc,$fp,$nc);
                                    fclose($fp);
                                    $this->write($nc,$cfile);
                                    echo $nc;
                        }
            }
            
            function incr($pc,$fp,&$nc)
            {
                        if($pc != "") 
                                    $nc = intval($pc)+1;
                        else 
                                    fputs($fp,1);
                        return $nc;
            }
            
            function write($nc,$cfile)
            {
                        $fp = fopen($cfile,"w");
                        $done = fputs($fp,$nc);
                        if($done == 0)
                                    exit;
                        fclose($fp);
            
            }
//END of counter functions
}
Additionally, why do I have to pass the nc variable to function incr()
as reference? It did not work just do return $nc.
 
Thomas

Reply via email to