2013-08-07 36 views
-1

我创建了一个离线Web应用程序并在缓存方法上工作...因为我使用jquery,它始终在更新,并且想要使用这些Cookie:如何包含已缓存到HTML的呈现的PHP文件

<link rel="apple-touch-icon" href="themes/images/apple-touch-icon.png"/> 
<link rel="apple-touch-startup-image" href="themes/images/tstartup.png" /> 
<meta name="mobile-web-app-capable" content="yes" /> 
<meta name="mobile-web-app-status-bar-style" content="black" /> 
<meta name="apple-mobile-web-app-capable" content="yes" /> 

所以,他们只是发送到IPhone ... 我需要服务器端PHP这样做,一切对我来说... 的问题是,如果它的缓存它的PHP文件的不计算... 所以这是我的问题,有没有办法让服务器预先计算网站然后存储它?

诗对不起我的英语

+0

将所有这些存储在名为'header-info.html'的文件中,然后使用'<?php include'header-info.html'; PHP文件不会被缓存,它们通常是在html请求中动态运行的。缓存一个PHP页面确实有点挫败目的,你可以使用.html代替。 – mawburn

+0

你不需要排除这些项目从非iphones它罚款,如果他们在那里为其他浏览器 – cmorrissey

回答

1

您仍然可以调用PHP代码和变量,即使在高速缓存文件。有一种方法可以“绕过”这一点。我不知道你缓存究竟如何,但使用PHP是这样的:

<?php 

    /*start caching*/ 
    ob_start(); 

    SOME PHP OR HTML CODE HERE 

    /*this will store "already executed code" in cache and clears the buffer*/ 
    $storecodeincache .= ob_get_contents(); 
    ob_end_clean(); 

    /*now at this point there is a piece of code we want to execute later, so we 
use the same variable, but we store store PHP code we want execute later like this:*/  

    $storecodeincache .= 'SOMEPHPCODE'; 

    /*we start regular caching again*/ 
    ob_start(); 

    SOME PHP OR HTML CODE HERE 

    /*we are finished, we want to store the rest*/ 
    $storecodeincache .= ob_get_contents(); 
    ob_end_clean(); 

    /*not neccessary, just when you call the script you see what has been cached*/ 
    echo $storecodeincache ; 

    /*write all cached content into the file*/ 
    $handle = fopen('safe://pathwhereyousavethecachedcontent', 'w'); 

    fwrite($handle, $storecodeincache); 

    fclose($handle); 

    ?> 

最重要的部分是$storecodeincache .= ob_get_contents();在beggining当我们停止缓存 - 此存储非执行 PHP代码到缓存的文件中 - 请注意,在这一点上,我们是“不缓存”,但我们将此代码存储到缓存文件anywa!因为我们做过

$storecodeincache .= ob_get_contents(); 
ob_end_clean(); 

哪个结束了缓存。我们正在做

ob_start(); 

之后(它开始再次缓存)。但在两者之间,PHP缓存已关闭。您可以在任何时候关闭PHP缓存,将任何未执行的 PHP代码存储到用于缓存“已执行代码”的相同变量中,然后继续(再次打开缓存并继续)。

+0

对不起,但是我将不得不把这个代码? 围绕PHP包括? (包括(“themes/header.html”);) – adho12

+0

在你的“缓存脚本”中使用这段代码 - 这段代码可以让你创建一个缓存文件,然后你将会包含这个文件。应该一次又一次地调用这个缓存脚本,以便每隔几分钟获取一个新版本的文件(使用服务器CRON)。如果你只需要在你的脚本中定期缓存,你可以在文件开始时使用'ob_start();'然后使用'ob_get_contents();'将内容保存到变量中,然后清除缓冲区'ob_end_clean();'然后使用'fwrite'将结果保存到其他的php文件中 - 该文件就像'result.php',它包含你想要的任何地方。 – Mordor

+0

谢谢 但我找到了另一种方式,以便我不必现金php – adho12

相关问题