나는 내 웹 사이트의 일일 사용자 수를 보여주는 통계 페이지를 만들지 만 문제가 발생했습니다. PHP 배열을 매 10 분마다 새로운 사용자 수로 업데이트하고 싶습니다.웹을 통해 연결하지 않고 PHP 코드 업데이트
내 목표는 클라이언트가 웹 페이지에 연결하고 나면 그래프를 쉽게 만들 수있는 최신의 전체 사용자 수를 이미 포함하게됩니다.
어떻게하면됩니까?
나는 내 웹 사이트의 일일 사용자 수를 보여주는 통계 페이지를 만들지 만 문제가 발생했습니다. PHP 배열을 매 10 분마다 새로운 사용자 수로 업데이트하고 싶습니다.웹을 통해 연결하지 않고 PHP 코드 업데이트
내 목표는 클라이언트가 웹 페이지에 연결하고 나면 그래프를 쉽게 만들 수있는 최신의 전체 사용자 수를 이미 포함하게됩니다.
어떻게하면됩니까?
정말 간단하고 중요하지 않은 것을 찾으려면 직렬화 된 배열을 사용하여 플랫 텍스트 파일에 저장하십시오 (그렇지 않으면 mySQL 테이블을 사용하는 것이 좋습니다).
은 다음과 같이 일할 수 :
<?php
class DailyVisitors {
protected $today, $statsFilePath, $dailyVisitors;
function __construct(){
$this->today = date('Y-m-d');
// A hidden file is more secure, and we use the year and month to prevent the file from bloating up over time, plus the information is now segmented by month
$this->statsFilePath = '.dailystats-' . date('Y-m-d');
// Load the file, but if it doesn't exists or we cannot parse it, use an empty array by default
$this->dailyVisitors = file_exists(statsFilePath) ? (unserialize(file_get_contents($statsFilePath)) ?: []) : [];
// We store the daily visitors as an array where the first element is the visit count for a particular day and the second element is the Unix timestamp of the time it was last updated
if(!isset($this->dailyVisitors[$this->today])) $this->dailyVisitors[$this->today] = [0, time()];
}
function increment(){
$dailyVisitors[$this->today][0] ++;
$dailyVisitors[$this->today][1] = time();
file_put_contents($this->statsFilePath, serialize($this->dailyVisitors)));
}
function getCount($date = null){
if(!$date) $date = $this->today; // If no date is passed the use today's date
$stat = $this->dailyVisitors[$date] ?: [0]; // Get the stat for the date or otherwise use a default stat (0 visitors)
return $stat[0];
}
}
$dailyVisitors = new DailyVisitors;
// Increment the counter for today
$dailyVisitors->increment();
// Print today's visit count
echo "Visitors today: " . $dailyVisitors->getCount();
// Print yesterday's visit count
echo "Visitors yesterday: " . $dailyVisitors->getCount(date('Y-m-d', strtotime('yesterday')));
을 당신이 그것을에게 새로운 방문자가 어쨌든있을 때마다 업데이트 할했듯이 만, 10 분마다 데이터를 표시 할 필요가있다 확실하지 않다, 그리고 데이터의 unserializing은 매우 빠르며 (한 자리 수 밀리 초 범위 내), 어떤 이유로 든 별도의 캐시 파일을 업로드할지 여부를 결정하기위한 타임 스탬프 (매일 배열의 두 번째 요소) 만 있으면됩니다 타임 스탬프 (유닉스 시대 이후 초 단위로 표현됨)에서 모듈러스 600 (10 분)을 사용하여 특정 날짜의 방문수 만 저장합니다.
크론 작업입니까? 하지만 요청에 따라 통계를 얻을 수있는 이유는 무엇입니까? – nogad