Write an article based on this "Create a new file called "HitCounter.php". Stub out the HitCounter class. Write the constructor. Write the public functions. Fill in the remaining helper methods."
This file will contain a class you can include in other PHP scripts when you want it to log a hit. Create local class members to hold your credentials and the database connection info. In the constructor, you should establish the database connection and initialize the hit counts at zero. These methods can be called from any other script that instantiates the HitCounter class. Add a method for processing views. This method gets called on every page load that should be counted towards a hit. Add a getter for the total views. This will get called in places where you want to show the total view count. Add a getter for the unique hits. You'll call this where you want to show the unique view count. These methods do the brunt of the work for the hit counter. They're marked private so that they can only be used internally. getData() retrieves the current view counts from the database. If there isn't any data, it sets this data to zero. isNewVisitor() determines whether the visitor has already visited our website in their current session visit() increments the total hit counter, and increments the unique visitor counter if the user has not visited the website in their current session.