Need help with a counter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tehgreatmg
    New Member
    • Jan 2007
    • 49

    #1

    Need help with a counter

    What I have is this php code saved as a .php file
    Code:
    <?php
    $count_my_page = ("hitcounter.txt");
    $hits = file($count_my_page);
    $hits[0] ++;
    $fp = fopen($count_my_page , "w");
    fputs($fp , "$hits[0]");
    fclose($fp);
    ?>
    And this in my html code

    Code:
    <?php
    include ("counter.php");
    ?>
    I have never done any webdesign before so I am stumped on how to get this to work. I got the code from some website, what I need is I have a server that does html, no asp or anything, and I need an invisible counter on the page that writes the hit count to a txt file. Any help would be appreciated, THANKS!!
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    Originally posted by tehgreatmg
    I have a server that does html, no asp or anything,
    PHP won't do anything for you in that case.

    assuming you can assign write access to a folder or file, you can use the client to do this via AJAX. this is not the most reliable process in the world, but it sure beats nothing.

    here is the simplest thing i could think of:
    Code:
    [B]
    <script>
    function IO(U, V) {
    X = !!window.XMLHttpRequest ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
    X.open(V ? "PUT" : "GET", U, !!0);
    X.send(V ? V : "");
    return X.responseText;}
    var oD=IO('hit.txt');
    IO('hit.txt', oD+'.')
    </script>[/B]
    the filesize of hit.txt = the number of hits.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, tehgreatmg.

      If your server has PHP installed, rename your page to have a .php extension and add the include statement at the top of your script.

      For example, if your page were called 'home.html', you'd rename it to 'home.php' and add the following code to the top of the page:

      [code=html]
      <?php
      include ("counter.php") ;
      ?>
      [/code]

      On the other hand, if (as you mentioned in your OP) you can't use anything other than HTML, you'll have to use an external script to update your hit counter. http://www.google.com/search?q=hit%20counters

      Comment

      Working...