Как и в комментариях сказать ..
Вы можете сделать это с помощью Javascript / Ajax. Причина этого заключается в том, что PHP запускается при загрузке страницы, поэтому он будет отображать только значение, найденное при загрузке страницы, поэтому, имея вызов базы данных в отдельном файле, вы можете колл с AJAX это путь.
Пример псевдокода.
PHP-файл
getCustomerCount.php
$servername = "localhost";
$username = "root";
$password = "12345678";
$dbname = "db";
if(isset($_POST['getCustomerCount']))
{
// Connection to database
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno())
{
echo json_encode(['success'=> false,'error_message'=>"NOT_OK"]);
//echo 'NOT_OK';
//echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$dbfile = "visitors.db"; // path to data file
$expire = 300; // average time in seconds to consider someone online before removing from the list
if(!file_exists($dbfile)) {
echo json_encode(['success'=> false,'error_message'=>"Error: Data file " . $dbfile . " NOT FOUND!"]);
die();
//die("Error: Data file " . $dbfile . " NOT FOUND!");
}
if(!is_writable($dbfile)) {
echo json_encode(['success'=> false,'error_message'=>"Error: Data file " . $dbfile . " is NOT writable! Please CHMOD it to 666!"]);
die();
//die("Error: Data file " . $dbfile . " is NOT writable! Please CHMOD it to 666!");
}
$count = CountVisitors($dbfile, $expire);
if(is_numeric($count)){
$out = sprintf("%03d", $count); // format the result to display 3 digits with leading 0's
echo json_encode(['success'=>'true', 'customer_count'=>$out]);
}
else
{
echo json_encode(['success'=> false, 'error_message'=>"count is not numeric"]);
}
}
else{
echo json_encode($_POST);
}
function CountVisitors() {
global $dbfile, $expire;
$cur_ip = getIP();
$cur_time = time();
$dbary_new = array();
$dbary = unserialize(file_get_contents($dbfile));
if(is_array($dbary)) {
while(list($user_ip, $user_time) = each($dbary)) {
if(($user_ip != $cur_ip) && (($user_time + $expire) > $cur_time)) {
$dbary_new[$user_ip] = $user_time;
}
}
}
$dbary_new[$cur_ip] = $cur_time; // add record for current user
$fp = fopen($dbfile, "w");
fputs($fp, serialize($dbary_new));
fclose($fp);
return count($dbary_new);
}
function getIP() {
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif(isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
else
{
$ip = "0";
}
return $ip;
}
Файл Javascript .
getCustomerCount.js
$(function(){
//Set interval function to update after set time
var timer = setInterval(function(){
updateCustomerCount();
}, 5000);
});
function updateCustomerCount()
{
$.ajax({
url:"getCustomerCount.php",
type:"POST",
data:{getCustomerCount:true},
success:function(response){
console.log(response);
var data = $.parseJSON(response);
if(data.success)
{
$("#customer_count span").text(data.customer_count);
}
else
{
console.log(data.error_message);
}
},
error:function(response){
console.log("Server Error");
}
});
}
HTML-файл .
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<p id="customer_count">Customers Online: <span></span></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="getCustomerCount.js"></script>
</body>
</html>
Это должно послужить вам примером того, как это сделать, но я советую очистить его и изучить различные части, чтобы посмотреть, как их можно улучшить для вашего использования.