开发者

Can I read an external file every set amount of time?

I am writing a script where it checks for an updated version from an external server. I use this code in the config.php file to check for latest version.

$data = get_theme_data('http://externalhost.com/style.css');
$latest_version = $data['Version'];
define('LATEST_VERSION', $latest_version);

This is fine and I can fetch the latest version (get_theme_data is WordPress function) but the problem is that it will be executed on every single load which I do not want. I also 开发者_JAVA技巧do not want to only check when a form is submitted for example. Alternatively I was looking into some sort of method to cache the result or maybe check the version every set amount of hours? Is such thing possible and how?


Here, gonna make it easy for you. Store the time you last checked for the update in a file.

function checkForUpdate() {
    $file = file_get_contents('./check.cfg', true);
    if ($file === "") {
        $fp = fopen('./check.cfg', 'w+');
        fwrite($fp, time() + 86400);
        fclose($fp);
    }
    if ((int)$file > time()) {
        echo "Do not updatE";
    } else {
        echo "Update";
        $fp = fopen('./check.cfg', 'w+');
        fwrite($fp, time() + 86400);
        fclose($fp);
    }
}

You can obviously make this much more secure/efficient if you want to.

Edit: This function will check for update once every day.


A scheduled task like this should be set up as a separate cron or at job. You can still write everything in PHP, just make a script that runs from the command line and does the updating. Checkout "man crontab" for details, and/or check which scheduling services your server is running.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜