How to Make a Hit Counter for Your Webpage?

  • Thread starter Thread starter Alamino
  • Start date Start date
AI Thread Summary
Creating a hit counter for a webpage can be achieved using a simple PHP script that reads and updates a counter stored in a text file. The provided script opens a file named "Counter.txt," reads the current hit count, increments it by one, and then writes the updated count back to the file. This method effectively tracks the number of visits to the webpage by displaying the updated hit count. While this approach is straightforward, there may be more efficient methods, such as utilizing server logs from Apache, which could simplify the process of tracking visits without needing to manage a separate file for the counter. Overall, this basic script serves as a foundational starting point for beginners in PHP and web development.
Alamino
Messages
69
Reaction score
0
I want to make a hit counter for a webpage. There are a lot of sites that "give" free counters, but I'm interested in knowing how to programm them.

Does anyone knows how to make one?
 
Computer science news on Phys.org
Here is a simple script in php

Code:
<?php
    $file=fopen("Counter.txt", "r+");
    $hits=fread($file,filesize("Counter.txt"));
    fclose($file) ;
    $hits+=1;
    $file=fopen("Counter.txt", "w");
    fwrite($file, $hits);
    fclose($file);
    // Print $hits in some nice fashion
    ?>

Basically, everytime someone accesses the page it opens a file, reads the last number, increaments the number, writes that number to a file, and displays that number on the webpage. I've never written a hit counter for a webpage before, so I don't know how effecient this method is, it just seems logical. Perhaps there is a way for apache to keep a log of all the people that go to your website, then all you have to do is read in the file and output it to the webpage.
 
Thanks for the script. I must confess I'm new in constructing webpages, so I don't know much php (well, I know almost nothing...). I will try to make the counter this way.

Thanks again.
 
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Back
Top