php - counting the site visitors not working -
i use these following codes in main index.php
of site number of visitors :
<?php $file = "counts.html"; if ( is_file( $file )==false ) {touch($file);$open = fopen($file, "w");fwrite($open, "0");fclose($open);} //read counts $open = fopen($file, "r"); $count = fread($open, filesize($file)); fclose($open); //if cookie isn't set,then increase counts 1 + save ip, , set cookie pc... $cookie_namee='mycounterr-456'; if (!isset($_cookie[$cookie_namee])) { $open = fopen($file, "w"); $count++; fwrite($open, $count); fclose($open); setcookie($cookie_namee,"checked",time()+111400); } //uncomment below line see visits number on page echo $count; ?>
it works, not working. there i'm doing in wrong way?
edit : found problem. seems code works, problem value won't overwrite existing value in counts.html
the file writes 0 first time , won't change 0 @ all.
how can fix problem?
any appreciated
thanks in advance
first of must add session_start();
in first line of codes.
it's better save counter numbers .txt
file .html
file.
now change codes following codes :
<?php session_start(); $counter_name = "counts.txt"; if (!file_exists($counter_name)) { $f = fopen($counter_name, "w"); fwrite($f,"0"); fclose($f); } // read counter values $f = fopen($counter_name,"r"); $counterval = fread($f, filesize($counter_name)); fclose($f); // check if visitor has been counted in session // if not, increase counter value 1 if(!isset($_session['hasvisited'])){ $_session['hasvisited']="yes"; $counterval++; $f = fopen($counter_name, "w"); fwrite($f, $counterval); fclose($f); } ?>
Comments
Post a Comment