• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP - I need a daily hits counter - no mysql please.

replace data.txt with the file name in which you want to store the counter's data. place them both in the same directory.
PHP:
<?
// counter.php
$f = "data.txt";
$fp = file($f); 
$i=0;
$new = $fp[$i] +1;
$newdata = fopen("$f", "w");
fputs($newdata,"$new");
echo "$new";
?>
 
Last edited:
Originally posted by kabatak
replace data.txt with the file name in which you want to store the counter's data. place them both in the same directory.
PHP:
<?
// counter.php
$f = "data.txt";
$fp = file($f); 
$i=0;
$new = $fp[$i] +1;
$newdata = fopen("$f", "w");
fputs($newdata,"$new");
echo "$new";
?>

don't use this for high traffic sites! it crashes the system or doesn't count properly and gets fux0rd
 
Originally posted by NONO
hi, doesn't it need to fclose() upon fputs($newdata,"$new") ?

thanks!
oh yes, put fclose($newdata); so that will be.
PHP:
<?
$f = "data.txt";
$fp = file($f); 
$i=0;
$new = $fp[$i] +1;
$newdata = fopen("$f", "w");
fputs($newdata,"$new");
fclose($newdata);
echo "$new";
?>
 
Originally posted by tandoc
don't use this for high traffic sites! it crashes the system or doesn't count properly and gets fux0rd
understand. if no database, are there others way to do that? I'd like to know too. :)
I look into my books (of course, a littile books :p), they teach us only this way. :(
thank you. :)
 
how aboot this? I kind of elaborated on the above

PHP:
<?php

 function count_hit()

 { 
   $file_pointer= fopen("hits.txt", "r+");

   

   if ($file_pointer == false)

   {

     return "Error: could not open the file! It may not exist!";

     exit; 

   }

   

   $hits= fread($file_pointer, filesize("hits.txt"));

   $hits= trim($hits);

   $hits++;

   fseek($file_pointer, 0);

   $result= fwrite($file_pointer, $hits);

   

   if ($result == false)

   {

     return "Error: could not write to the file!";

     exit; 

   }

   else {

     return $hits;

   }

   

   $close= fclose($file_pointer);

   

   if ($close == false)

   {

     echo "Error: could not close the file!";

     exit; 

   }

 }

?>
 
Back
Top