包含在另一个包含的PHP文件中打开但关闭条件的PHP文件

包含在另一个包含的PHP文件中打开但关闭条件的PHP文件,php,caching,php-include,phpfastcache,Php,Caching,Php Include,Phpfastcache,我想在我的脚本的开头包含一个打开IF条件的PHP文件。然后我编写我的脚本,最后我包含了另一个关闭条件的PHP文件 这导致了一个“解析错误:语法错误,意外的文件结尾在…”错误 通过这个简单的示例,您可以更好地理解这一点: header.php if(aConditionalTest()) { } // endIf include_once 'header.php'; echo 'my awesome content'; include_once 'footer.php'; use phpF

我想在我的脚本的开头包含一个打开IF条件的PHP文件。然后我编写我的脚本,最后我包含了另一个关闭条件的PHP文件

这导致了一个“解析错误:语法错误,意外的文件结尾在…”错误

通过这个简单的示例,您可以更好地理解这一点:

header.php

if(aConditionalTest()) {
} // endIf
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
footer.php

if(aConditionalTest()) {
} // endIf
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
mypage.php

if(aConditionalTest()) {
} // endIf
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
仅供参考:我想这样做,例如:

  • 在显示内容之前,检查用户是否已获得授权
  • 实现网页缓存系统(参见“示例”部分“缓存整个网页”)
谢谢


编辑:更准确地解释我为什么要使用phpfastcache执行此操作:

说:

缓存整个网页PHP缓存整个网页: 您也可以使用phpFastCache轻松缓存整个网页。这很简单 例如,但在实际代码中,应将其拆分为2个文件: cache_start.php和cache_end.phpcache_start.php将存储 在ob_start()之前开始代码;而cache_end.php将从 获取HTML网页。然后,您的index.php将在 开始并在文件末尾缓存_end.php

这正是我想做的! 根据他们下面的一段代码,这会导致在“cache\u start.php”中打开条件,然后在“cache\u end.php”中关闭条件的情况

cache_start.php

if(aConditionalTest()) {
} // endIf
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
cache_end.php

if(aConditionalTest()) {
} // endIf
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
mypage.php

if(aConditionalTest()) {
} // endIf
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
myotherpage.php

if(aConditionalTest()) {
} // endIf
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
 use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
 // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expireAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
所以我想把phpfastcache代码放在两个单独的文件中的原因是我有许多不同的PHP页面要缓存,所以我想避免在每个页面上重复所有这些代码

希望此编辑将帮助您更好地理解我为什么要这样做,即使我知道,正如我担心的那样,这是不可能的。

尝试一下:

我怎样才能做到这一点

用邪恶的方式去做,评估一切,而不是包括:)就像

eval(file_get_contents('header.php').''.file_get_contents('footer.php'));
如果你想加入黑暗面,这可能是一个解决方案:)

旁注:在这个解决方案中,您必须关注全局变量

但请注意,事实上,你们想把条件分散在不同的文件上,我认为这是非常糟糕的做法


我真的回答了这个问题吗8]

所以我知道问题是当你不希望某个用户看到某个页面的内容时,页面的其余部分没有加载,这就是导致错误的原因

如果是这样的话,为什么你不总是包括文件,并在特定的页面中为谁可以查看什么设置条件呢

我在页眉中使用ob_start(),在页脚中使用ob_end_flush,效果非常好。 然后检查特定页面内容上的my SESSION variables(我的会话变量),如果登录用户有权查看该内容,则会显示一条消息,如:“您无权查看此内容”

我以其他方式尝试

唯一规则:仅在全局空间中有效(其他位置:-)

因此,您希望在
cache\u start.php
中打开一个if(),然后将其关闭
cache\u end.php
。(出于ob_缓存原因)

但是如果条件没有改变,为什么不做两次呢

在每个文件中测试
if(条件)

或者设置一个变量,如
$cach\u op\u start=true
,并在
cache\u end.php中的第二个if()中对其进行测试

这两件事都应该适合你

有点可笑的是,我第一次没有看到这个解决方案:)

最后一点: 如果愿意,还可以在PHP中使用自动前置和追加文件。 可以在php.ini中配置。 这些文件将始终在脚本前后自动加载。


祝你玩得愉快。

哎哟。。。这是行不通的。问题是什么?问题是“我怎样才能做到这一点?”请参阅phpfastcache链接以了解。我没能按他们的建议去做…你想要的是不可能的。结束。补充其他人所说的。这是不可能的。从目前的情况来看,您可能应该重构。你需要在一个单独的PHP文件中使用这个条件是有原因的吗?是的,我同意,这真的是很糟糕的编程。这是“天哪,我会害怕尝试和维护它。让我们把这些垃圾扔进垃圾桶,重新开始”的代码。我不会走到黑暗的一面…:)@DataHerder当然不好。。。但是我的+1是因为我有勇气这样使用
eval()
。@Wh1T3h4Ck5我之所以放弃它,是因为OP评论
“我怎样才能做到这一点?”
。因此,我给出了一个直截了当的回答。:-)哦,伙计,当PHP7是主要使用的PHP版本时,我们在这里都会玩得很开心但是thanx说到点子上。@DataHerder:-)@DataHerder看,它是活着的:-)@toolphone.NeT第二个代码块很清楚,第一个短端版本,确实不清楚。在我了解了所有关于ob_cache的内容后,很容易回答。谢谢,这是一个非常简单的解决方案,效果非常好!我在包含“开始”的PHP文件中放置了一个全局布尔值,并将其用于“结束”PHP文件中的条件,以及我希望缓存的“内容”PHP文件中的条件。。。不知道为什么我没有自己考虑……:)