I need a litte help getting this simple hit counter to work on one of my web pages. My "counter.php" file contains only the following code:

<?php
$filename = "counter.txt" ;
$fp = fopen($filename,"r") or die ("Can't open $filename") ;

$fstring = fread($fp, filesize($filename)) ;
echo "$fstring" ;
fclose($fp) ;

$fp1 = fopen($filename,"w") or die ("Can't open $filename") ;

$fcounted = $fstring + 1 ;
$fout = fwrite($fp1,$fcounted) ;
fclose($fp1) ;
?>

The web page where I am trying to show the number of hits contains this code:

		<p align="center"><font color="#2E8B57">Page Hits: <font color="ff0000">
			<script language="JavaScript" src="http://www.mywebsite.com/counter.php" type="text/javascript">
			<!--//-->
			</script>
			</font></p>

I get no error messages, just no hit number on my web page. The "counter.txt" file contains only one number and it does increment upward each time the web page gets a hit. I have tried taking out the quotes around the $fstring in the echo statement, but that didn't help.

    Since you are calling the counter file as a JavaScript source:

    echo "document.write('$fstring');";
    

      or replace

      <script language="JavaScript" src="http://www.mywebsite.com/counter.php" type="text/javascript">
      			<!--//-->
      			</script>
      

      with

      <?PHP echo $fcounted; ?>
      

        Thanks NogDog and rulian. I've tried both of your ideas and still get nothing. It has got to be something simple I'm doing wrong here. I've been pulling my hair out on this one and there is little hair left!!

          What about

          <?php
          $filename = "counter.txt" ;
          $fp = fopen($filename,"r") or die ("Can't open $filename") ;
          
          $fstring = fread($fp, filesize($filename)) ;
          echo 'var daHits = '.$fstring.';';
          fclose($fp) ;
          
          $fp1 = fopen($filename,"w") or die ("Can't open $filename") ;
          
          $fcounted = $fstring + 1 ;
          $fout = fwrite($fp1,$fcounted) ;
          fclose($fp1) ;
          ?> 
           <p align="center"><font color="#2E8B57">Page Hits: <font color="ff0000">  <script language="JavaScript" src="http://www.mywebsite.com/counter.php" type="text/javascript">
          document.write(daHits);
          </script></font></p>

            Sorry Kudose, that didn't work either.

            Do you think maybe one or more of these suggested solutions might be working but that the HTML page is over writing it by the time it is fully loaded? I don't know how to test for that to see if that might be the case.

            Is there any other way to bring the $fstring into my HTML web page without using a JavaScript script? I don't really want this particular web page to be a php file.

              Here is a surprise. I finally got it to work in a very mysterious way. I went back and used NogDog's recommended change to my php file using his echo "document.write('$fstring');"; since it seemed most logical. However, I forgot to change my HTML file which had Kudose recommended change of document.write(daHits) but all of a sudden it worked.

              I'll soon call this question resolved, but why it works I have no idea since "daHits" isn't even in my php file any longer.

                Write a Reply...