【问题标题】:PHP headers expiration checkPHP标头过期检查
【发布时间】:2012-03-12 11:26:09
【问题描述】:

我创建了一个自定义应用程序,我喜欢将一些 PHP 变量传递给 CSS。

我使用的方法是提供 CSS 标头的 style.php 文件,因此 HTTP 请求将请求作为普通 CSS 文件处理。

这种方法的问题在于,对于每个请求,我都必须在内存中加载一些大型 PHP 文件。

我喜欢做的是检查文件的过期日期,如果文件还没有过期,那么不要运行脚本,而是发送一个缓存文件。

那么,有什么办法可以解决这个问题吗?

我喜欢的 php 文件看起来像这样:

<?php
    // Enter here several headers to cache the file and other operations
    // Check here if the file must be re-executed or load a cached version instead
    // Enter here the inclution of my application scripts with include_once(....)
?>
body
{
    background-color: <?php echo $options->css['bg_color']; ?>
}

亲切的问候 梅里亚诺斯·尼科斯

【问题讨论】:

  • 您的 CSS 多久更改一次?通常,在部署应用程序时重新创建样式表会更容易,因为在部署之间它们不会发生任何变化。
  • 检查什么文件的到期日期?您希望如何维持到期日期?
  • 我不是指文件的过期时间。将为 CSS 提供服务的 php 文件将有 headers(....) 用于过期,我喜欢检查 headers 是否过期。
  • 我也很抱歉,我真的不确定你的意思。听起来您要求服务器决定是否应根据标头生成 CSS,但服务器是设置标头的对象。它不知道它过去发送了哪些标头。实际上,您可以只设置标头到期日期,然后您的客户端浏览器不应该再请求 CSS 文件。您的 PHP 脚本,直到它过期(或者他们清除浏览器缓存)。这有意义吗?
  • 是的,这是正确的 :( xaxaxa :D 很抱歉,我无法更好地描述我的问题,但我认为这就是我的意思。非常感谢@deed02392

标签: php caching header http-headers


【解决方案1】:

我不得不做一些类似于你正在做的事情,并解决了解析 CSS 文件服务器端的问题。这是一个简单的类的link,它就是这样做的(这是我现在使用的)。基本上它会解析目录中的所有文件并使用 strtr() 替换 css 文件中的值

class Css {
    //this is an associative array that's used when replacing
    private $settings;
    //This is the path (relative to this file) to the dir that holds the css files to parse
    private $cssDir = "css/";
    //private varray to store the files
    private $cssFiles = array();
    //This is the path (relative to this file) to the dir where the parsed css files wil be written
    private $outCssDir = "../css/";

    /**
     * the class thake only one parameters, an associative array of keys to replace with their respective values
     */
    function __construct($settings) {
        $this -> settings = $settings;
        $this -> readFiles();
        $this -> replaceValuesInCssStrings();

    }

    private function readFiles() {
        $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $this -> cssDir;
        if (file_exists($dir)) {
            if ($handle = opendir($dir)) {
                /* This is the correct way to loop over the directory. */
                while (false !== ($entry = readdir($handle))) {
                    //filter out .. and .
                    if ($entry !== "." && $entry !== "..") {
                        $cssFiles[$entry] = file_get_contents($dir . $entry);
                    }
                }

                closedir($handle);
            }
        }
        $this -> cssFiles = $cssFiles;

    }

    private function replaceValuesInCssStrings() {
        foreach ($this->cssFiles as $fileName => $fileContent) {
            $this->cssFiles[$fileName] = strtr($fileContent, $this-> settings);

        }
    }
    /**
     * renders the parsed css files in a <style> tag
     */
    public function render() {
        $css = "<style>\n";
        foreach ($this -> cssFiles as $fileContent) {
            $css .= $fileContent;
        }
        $css .= "</style>";
        echo($css);
    }

    /**
     * writes the parsed css files to the $outCssDir (keeping their names intact)
     */
    public function renderToFile() {
        $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $this -> outCssDir;
        if (file_exists($dir)) {
            foreach($this->cssFiles as $fileName => $fileContent){
                file_put_contents($dir . $fileName, $fileContent);

            }   
        }
    }
    /**
     *  this is just in case you want to call echo ($css)
     */
    public function __toString() {
        $this -> render();
    }

}

【讨论】:

  • 这是一个非常好的方法,我已经在我制作的另一个 Web 应用程序中使用了它。我现在创建的那个,我需要将 CSS 写入 php 文件,并立即设置好值。我更改了上面的代码以向您展示我的意思。无论如何,谢谢你的代码! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 2012-12-08
  • 2011-10-02
相关资源
最近更新 更多