You can create an ajax file in PHP and call ajax in a time interval and display hit counter to page.
For this first of all create an ajax file: ajax_user_count.php
<?php
session_start();
$log_file_name = 'traffic_count.log';
$count = file_get_contents($log_file_name, true);
if(empty($count)){$count = 0;}
if(isset($_POST['action'])){
$action = $_POST['action'];
if($action == 'enter'){
if(!isset($_SESSION['user_count'])){
$count += 1;
if($count == 0) { $count = 1; }
$_SESSION['user_count'] = $count;
$message = $count;
file_put_contents($log_file_name, $message);
}
} else if($action == 'leave'){
$count -= 1;
$_SESSION['user_count'] = $count;
$message = $count;
file_put_contents($log_file_name, $message);
session_destroy();
}
}
echo $count;
die;
Then call the ajax file by this
$(document).ready(function(){
get_enter_web_traffic();
setInterval(function(){
get_enter_web_traffic();
}, 1000);
$(window).bind("beforeunload", function() {
$.ajax({
type: "POST",
async: false,
cache: false,
url: "ajax_user_count.php",
data: {'action' : 'leave'},
success: function(result) { },
error: function(data) { location.reload();
}
});
});
});
function get_enter_web_traffic(){
var data = {'action' : 'enter'};
$.post('ajax_user_count.php',data,function(response){
$('#count_user').html(response); // website hit count
});
}