PHP Web Site Counter?

Discussion in 'PHP' started by HarryA, Mar 31, 2023.

  1. #1
    Greetings from Pennsylvania. I am trying to add a counter to a web page
    but it is not cooperating!
    Directions are: place the count.php file and the hitcount.txt file on the server in the
    same directory as the web page. hitcount.txt file should contain a value like zero.
    For the include in the web page I use:

    <div class="footnote">
    <?php include‘count.php’; ?>
    </div>

    Things I have tried: change permissions on both files to 777. Change the web site
    file extension to stml from html. The error logger on the server shows no new errors.
    I ran a install.php to install a forum on the server last week so I know php files
    will run in my server space. Could someone with knowledge of php please look at this code?

    $filename = "hitcount.txt"; // This is at root of the file using this script.
    $fd = fopen ($filename, "r"); // opening the file counter.txt in read mode
    $contents = fread ($fd, filesize($filename)); // reading the content of the file
    fclose ($fd); // Closing the file pointer
    $contents=$contents+1; // incrementing the counter value by one
    echo $contents; // printing the incremented counter value

    $fp = fopen ($filename, "w"); // Open the file in write mode
    fwrite ($fp,$contents); // Write the new data to the file
    fclose ($fp); // Closing the file pointer
     
    HarryA, Mar 31, 2023 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,255
    Likes Received:
    1,690
    Best Answers:
    31
    Trophy Points:
    475
    #2
    What's up with that include? Shouldn't it be like this?

    <div class="footnote">
    <?php include ("count.php"); ?>
    </div>
     
    qwikad.com, Mar 31, 2023 IP
  3. HarryA

    HarryA Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks. It is still unhappy. I will try a perl counter; the problem is switching to .stml
    will require me to create dummy .html file to redirect it to the .stml I gather. :(
     
    HarryA, Mar 31, 2023 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,255
    Likes Received:
    1,690
    Best Answers:
    31
    Trophy Points:
    475
    #4
    This counter works for me:


     $filename = "hitcount.txt";
         $count = file_get_contents($filename);
         if ($count == null)
            $count = 0;
         echo $count;
         $count++;
         $handle = fopen($filename, "w+");
         fwrite($handle, $count);
         fclose($handle);
    Code (markup):
     
    qwikad.com, Mar 31, 2023 IP
  5. HarryA

    HarryA Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    I back like pesty fly. Thanks for the code. I am still dorking around try to get a counter to count.
    I see in O'REILLY "Programming PHP" they embed the php code into the html file so I am trying that
    but I do not see any count. Please look at the source at: http://www.bobbear.org/TestWebSite/TestWebSite.html
     
    Last edited by a moderator: Apr 5, 2023
    HarryA, Apr 2, 2023 IP
  6. dascos

    dascos Member

    Messages:
    11
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    33
    #6
    count.php is in the same directory as index.php and in index.php you want to "include" count.php, which open a file, read its content and display the value, right?
    So, to avoid misconfigurations, use this
    
    include __DIR__ . "/count.php";
    
    Code (markup):
    Same for count.php on opening hitcount.txt.

    This should work but better if you can post the directory tree like, eg,
    
    web_directory
    ├── subfolder
    │ ├── counter.php
    │ └── hitcount.txt
    └── index.php
    
    Code (markup):
     
    dascos, Apr 14, 2023 IP
  7. HarryA

    HarryA Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Thank you all. I found that
    <!--#include virtual='counter.php' --> works.
    New page: http://bobbear.netfirms.com/PHPTestWebSite2/PHPTestWebSite.shtml see try 0.
    I am going to stay away from the server side, they may have cookies there but it is to complex there for me. I am going back to embedded C programming and stay there!
     
    HarryA, Apr 14, 2023 IP