- PHP page hits counter script using text file
- $filename = «counter.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 /* The above code will do the reading and displaying the counter to the screen, now we have to store the above value in the same counter.txt file by overwriting the old data with the new counter data. We will open the counter.txt file in write mode and then write to it. */
- Saved searches
- Use saved searches to filter your results more quickly
- License
- JulianLaval/php-hit-counter
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Simple Page Hit Counter in PHP
- Simple Web Page Hit Counter Code Using PHP and MySQL
- The Code
- Counter Code Tips
PHP page hits counter script using text file
Counters are required to count the number of visitors coming to the page or the browsers open the number of time the page. The PHP script we will be discussing here is one simple page counter and it will not take in to account if same visitors in same session opening the page again and again. This will also not check the IP and increase the count with unique IP only. These are part of advanced counter script we will discuss separately. We will discuss here one simple page counter or hit counter. In the second part of the script we will discuss graphical counter in which we will discuss on how to manage attractive displays showing the count value.
We will use flat text file to store the data ( count of the page ) and will not use any database here for simplicity.
For a better understand on how to read from a file and on how to write to a file, please read the articles here before working on the counter script.
We will use the page counter.php to store our script and we will keep our data ( counter value ) in the text file counter.txt. Both the files will be in same directory.
Create one text file in the same directory of the page where the counter to be displayed and give the name counter.txt. If you are using this script in Linux system then give write permission to this file. If you are testing this script in your local window machine then this may not be required as by default write permission will be there. If you are running this script in a windows hosting account then ask the administrator to give write permission to this file. This is important as we will be writing this file with new data on every page request of the main file.
Here is the code to open the counter.txt file and then displaying the data by incrementing it by one. We will use file read function fread() here to read the data from the text file. Before that we will use fopen to open the file in reading mode.
$filename = «counter.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
/*
The above code will do the reading and displaying the counter to the screen, now we have to store the above value in the same counter.txt file by overwriting the old data with the new counter data. We will open the counter.txt file in write mode and then write to it.
*/
$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
That’s all, very simple. You can create counter for any page like this. But we will move to next step. We will try to create attractive graphical counter or call it digital counter using the same script but adding little more to display images in place of numbers digits. Like this.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Simple hit counter for tracking website traffic.
License
JulianLaval/php-hit-counter
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Simple hit counter for tracking website traffic, using PHP (PDO) and a MySQL database to store information.
- Track hits per page
- Track total hits
- Track total unique visitors
- Track IP, user agent, and timestamp information
- View consolidated information with tables and graphs
Add this snippet to the page you want to track:
require_once('conn.php'); require_once('counter.php'); updateCounter("page name"); // Updates page hits updateInfo(); // Updates hit info
Tested with PHP 5.4.16 and MySQL 5.1.72
- Make sure you have PHP Data Objects (PDO) enabled.
- Open up conn.php and add your database information where required.
- Run install.php . A message will be displayed if the installation was successful.
- Delete install.php .
- Add relevant code to the page you wish to track.
- Open view.php to view consolidated information (last 10 IP entries will displayed).
- Inspired by PHP — Page hit counter
- Includes the Dygraphs open source JavaScript library for graph rendering
About
Simple hit counter for tracking website traffic.
Simple Page Hit Counter in PHP
To create a simple page hot counter what we need is a mysql table to store the hits and then a PHP script to send the hits to the mysql table and retrieve the hits back.
We can use CREATE TABLE statement to create the table in the mysql database.
Let’s create the table in mysql using this statement:
CREATE TABLE `page_hit_counter_table` (`page_hit_counter` INT(255) NOT NULL);
We have the page_hit_counter column set as NOT NULL. Let’s set the value for the column to 0 so that we can increment it every time an user visits the page.
INSERT INTO page_hit_counter VALUES (0);
The page hit counter column is ready to go and increment.
Let’s add the script to increase the hit counter.
Let’s add a code to make a connection to the database.
mysql_connect("your mysql host address", "username", "password") or die(mysql_error()); mysql_select_db("Your Database Name") or die(mysql_error());
Let’s add code to increase the page hit counter. We’ll use UPDATE statement like any sql (postgresql, mysql etc.).
mysql_query("UPDATE page_hit_counter_table SET page_hit_counter = page_hit_counter + 1");
Let’s retrieve the counter and print it on the blog post page (or any kind of page that you have the counter for).
$page_hit_counter = mysql_fetch_row(mysql_query ("SELECT page_hit_counter FROM page_hit_counter_table")); echo $page_hit_counter[0];
DONE. It should display the page hit counter on your page now.
Simple Web Page Hit Counter Code Using PHP and MySQL
Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms.
Website stats provide important information to a website owner about how the site is doing and how many people visit. A hit counter counts and displays how many people visit a webpage.
The code for a counter varies depending on the programming language used and the amount of information you want the counter to collect. If you, like many website owners, use PHP and MySQL with your website, you can generate a simple hit counter for your webpage using PHP and MySQL. The counter stores the hit totals in a MySQL database.
The Code
To get started, create a table to hold the counter statistics. Do that by executing this code:
CREATE TABLE `counter` ( `counter` INT( 20 ) NOT NULL );
INSERT INTO counter VALUES (0);
The code creates a database table named counter with a single field also called counter, which stores the number of hits the site receives. It is set to start at 1, and the count increases by one each time the file is called. Then the new number is displayed. This process is accomplished with this PHP code:
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
//Adds one to the counter
mysql_query("UPDATE counter SET counter = counter + 1");
//Retrieves the current count
$count = mysql_fetch_row(mysql_query("SELECT counter FROM counter"));
//Displays the count on your site
print "$count[0]";
?>
This simple hit counter doesn’t give the website owner valuable information such as whether the visitor is a repeat visitor or a first-time visitor, the location of the visitor, which page was visited, or how much time the visitor spent on the page. For that, a more sophisticated analytics program is necessary.
Counter Code Tips
Wanting to know the number of people who visit your site makes sense. When you are comfortable with the simple counter code, you can personalize the code in several ways to work better with your website and gather the information you seek.
- Customize the database, table, and code to include other information
- Hold the counter in a separate file and retrieve it using include ()
- Format the counter text using regular HTML around the include function
- Create different rows on the counter table for additional pages on your website