首页 > 解决方案 > PHP 函数包括 -> 网站未加载

问题描述

当我尝试调用包含在整个站点上方几行的函数时,无法重新加载。

$object = 'termin';
$cache_available = check_cache($object);

这就是我调用函数的方式,定义如下所示:

function check_cache($object){
    $file = '../../cache/' . $object . '.json';
    if(file_exists($file)){
        $time_cache = date ("H:i:s", filemtime($file));
        $time_now = date ("h:i:sa");
        $diff = strtotime($time_now) - strtotime($time_cache);

        if($diff <= 60) {return true; } //Neu genug
        else            {return false;} //Zu alt
    }
    return false;
}

有人可以告诉我是什么导致了错误。

未加载的解释:---当我导航到浏览器中的文件夹(localhost,xampp)并单击文件以打开它时会加载一小段时间,但随后会再次显示该目录。

标签: phpfunction

解决方案


那 check_cache 看起来不正确。它有错误的路径检查开始和一个很长的返回语句。用 if 和 else。我重写了它,尝试一下,看看它是否有效。

function check_cache($object) {
    $file = __DIR__ . '/../../cache/' . $object . '.json';

    if(file_exists($file)){
        $time_cache = date ("H:i:s", filemtime($file));
        $time_now = date("H:i:s");
        $diff = strtotime($time_now) - strtotime($time_cache);

        return $diff <= 60;
    }

    return false;
}

推荐阅读