Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with counter

Status
Not open for further replies.

cyberwolf14

Programmer
Aug 27, 2001
65
BE
Hello,
I have problems with my self-made counter sometimes it resets all by itselfs this is the code

<?
function viewfile($filename){
$fd = fopen ($filename, &quot;r&quot;);
return fread ($fd, filesize ($filename));
fclose ($fd);
}
function writefile($filename,$contents){
$fd = fopen($filename, &quot;w+&quot;);
fwrite($fd , $contents);
fclose($fd);
}

writefile(&quot;bancount.txt&quot;,viewfile(&quot;bancount.txt&quot;)+1);
$ll = viewfile(&quot;bancount2.txt&quot;) . &quot;\n&quot; . viewfile(&quot;bancount.txt&quot;);
writefile(&quot;bancount2.txt&quot;,$ll);
echo viewfile(&quot;bancount.txt&quot;);
?>

What did I do wrong, can you help me ????
 
I dont know why your conter are doing that. But try my counter, It's working perfectly on my page( (not fully uploaded yet))

here is the source: Just remeber to make a file named 'counter.txt' in the same folder as your script

<?
//------------------------------------
// Counter File
//------------------------------------

$CounterFile = &quot;counter.txt&quot;; //The counter file.

//------------------------------------
// DO NOT EDIT ANYTHING FROM HERE
//------------------------------------
$fp = fopen($CounterFile, &quot;r+&quot;);
$count = fread( $fp, filesize($CounterFile));
$count += 1; //adding 1 to the counterfile
fclose($fp);

$fp = fopen($CounterFile, &quot;w+&quot;);
fputs($fp, $count);
fclose($fp);
include($CounterFile); //Putting number of visits on screen
}
?>
 
i'm having problems with my counter too.evrytym page is refreshed, the counter increments..seems that the cookie is not set..........help!!!!!
<?
$COUNT_FILE = &quot;./counter/count_data.txt&quot;;
$IMG_DIR_URL = &quot;./counter/digits/&quot;;
$NB_DIGITS = 8;
$EXPIRE_DATE = 86400;
if (file_exists($COUNT_FILE)) {
$fp = fopen(&quot;$COUNT_FILE&quot;, &quot;r+&quot;);
flock($fp, 1);
$counting = fgets($fp, 4096);
if ($attempt == &quot;&quot;) {
$counting += 1;
setcookie(&quot;attempt&quot;, $counting, time()+$EXPIRE_DATE , &quot;/&quot;, $SERVER_NAME);
fseek($fp,0);
fputs($fp, $counting);
}
flock($fp, 3);
fclose($fp);
} else {
echo &quot;Can't find file, check '\$file' var...<BR>&quot;;
exit;
}

chop($counting);
$nb_digits = max(strlen($counting), $NB_DIGITS);
$counting = substr(&quot;0000000000&quot;.$counting, -$nb_digits);

$digits = preg_split(&quot;//&quot;, $counting);

for($i = 0; $i <= $nb_digits; $i++) {
if ($digits[$i] != &quot;&quot;) {
$counter_disp .= &quot;<IMG SRC=\&quot;$IMG_DIR_URL$digits[$i].gif\&quot;>&quot;;
}
}
?>
 
What version of PHP are you using? Is register_globals set to on?
You could try changing
Code:
    if ($attempt == &quot;&quot;) {
to
Code:
    if (!isset($_COOKIES['attempt'])) {
//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top