drzablspw01655860 2017-01-19 02:19
浏览 17

更新PHP代码而不通过Web连接

I'm creating a statistics page which shows the daily user count of my website however I've ran into an issue. I'd like to update a PHP array every ~10 minutes with a new user count number.

My objective is once a client connects to the webpage it'll already contain a full, up to date, array of user counts to make a graph with easily.

How would I do this?

  • 写回答

1条回答 默认 最新

  • dongshui9690 2017-01-19 05:13
    关注

    Use a serialized array and store it in a flat text file if you're looking for something really simple and non-critical (otherwise I'd recommend using a mySQL table).

    It could work like this:

    <?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')));
    

    I'm not sure there's any need to only display the data every 10 minutes, as you've to update it every time there's a new visitor anyway, and the unserializing of the data is pretty fast (in the single digit milliseconds range), but if for some reason you need to, you can just the timestamp (2nd element of the array of each day) to determine whether to upload a separate cache file that only stores the visit count for a particular day, using a modulus 600 (10 minutes) on the timestamp (which is expressed in seconds since the Unix epoch).

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序