# Hit counter does not work

**URL:** <https://www.phphelp.com/t/hit-counter-does-not-work/35857>\
**Category:** Beginners - Learning PHP\
**Created:** [November 23, 2023, 2:51pm UTC](https://www.phphelp.com/t/hit-counter-does-not-work/35857 "2023-11-23T14:51:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phpdummy1](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@phpdummy1](https://www.phphelp.com/u/phpdummy1)\
**Post date:** [November 23, 2023, 2:51pm UTC](https://www.phphelp.com/t/hit-counter-does-not-work/35857/1 "2023-11-23T14:51:07Z")

</div>

Hello everyone,  
I try to use a hit counter which used to work years ago. However my transfer to a new file fails.

My trial:  
I renamed the functioning html code (whose hits shall be counted) to “filetocount.php”. It is situated in “httpdocs”.

In “filetocount.php” I inserted the following code directly before `</body>`.

.

```auto
<?php
include "../PHP/funktionen.php"; 
	 $hit=$_GET['hit'];
	 $druck=0;
	 zaehlerplus("ScilabSite_Hits.txt",$_SERVER['REMOTE_ADDR'],$druck,$hit);
?>

```

The file “funktionen.php” is located in “httpdocs/PHP”

Its code is

```auto
<?php
function zaehlerplus($fn,$ip,$druck,$hit)
{
/* Zugriffs-Zähler mit Zeit und IP-Adresse/counter with time and IP*/   
   $tstp=time('void');
   $Datum=date('d.m.o',$tstp);
   $Uhrzeit=date('H:i',$tstp);
   $ServAdd = gethostbyaddr($ip);

   if (file_exists($fn))
   {
     /* read all lines to get to last one */
     $fp = fopen($fn,"r");
	 $i=0;
	 $z[0]="";
      do
      {
	  $i=$i+1;
         $zeile = fgets($fp, 100);
		 $z[$i]=$zeile;
      }  
	  while (!feof($fp));
	  
	  $zeile=$z[$i-1]; /*Letzte Zeile*/
	  $worte = explode(" ",$zeile);  
	  $zahl=$worte[0]; /*Alter Zählerstand/old counter value*/ 
	  fclose($fp);
   }
   else
   {
      /* erster Zugriff ohne txt-file/ first hit without existing txt.file*/
      $zahl = 0;
   }

   $zahl = $zahl + 1; /*Neuer Zählerstand/New counter value*/ 
  
   $zeile=$zahl." visits IP: ".$ServAdd." Zeit: ".$Datum." um ".$Uhrzeit; 
   if($hit=='selbst'){$zeile=$zeile.' Selbstaufruf';} /* my hit */
      
   if ($druck){print($zeile);}

   /* Neue Zeile schreiben/write new line */
      $nl = chr(13) . chr(10); /*Hilfsvariable/auxiliary für line feed und carriage return*/
   $fp = fopen($fn,"a");
   flock($fp,2);
   fputs($fp,"$zeile$nl");
   flock($fp,3);
   fclose($fp);
}
?>

```

Can anyone give me a clue what should be changed?

Regards  
phpdummy1
