The problem is that the variable $views
does not persist from view to view. In fact, the next time someone comes back to your website $views
would have been reset to 0. You'll need to take a look at some form of persistence to store the total number of views.
One way that you can accomplish this is to use a database or via a file. If you are using files, you can do the following inside of your views.php file.
views.php
$views = 0;
$visitors_file = "visitors.txt";
// Load up the persisted value from the file and update $views
if (file_exists($visitors_file))
{
$views = (int)file_get_contents($visitors_file)
}
// Increment the views counter since a new visitor has loaded the page
$views++;
// Save the contents of this variable back into the file for next time
file_put_contents($visitors_file, $views);
main.php
include("views.php");
echo $views;