在包含在另一个页面中的文件中添加Expires Header

时间:2012-05-16 04:28:20

标签: php caching header

我有两个两个php页面。一个是主要的,包括第二个,我想为它添加一个过期的标题,但我得到以下错误

    Warning: Cannot modify header information -
 headers already sent by (output started at.. 

在第一个

ob_start('ob_gzhandler', 6);

/// lots of html here

include("pageTwo.php");

/// lots of html here

ob_end_flush();

在第二页

ob_start();
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));

/// lots of html here

ob_end_flush();

3 个答案:

答案 0 :(得分:1)

在应用压缩之前,请勿使用模式6并尝试

<?php
ob_start();

// include ...

ob_end_flush();
?>

答案 1 :(得分:0)

您需要在php页面生成任何输出之前发送标题。

这将工作:

<?php 
     print 'test';
     header( 'Expires: .... ' );
?>

这将有效:

<?php
     header( 'Expires: .... ' );
     print 'test';
?>

所以基本上你需要改变哪个页面标题。

答案 2 :(得分:0)

你不能在标题之前放置任何echos字符串,因为标题首先被认为是第一个而不是常规文本。您可以添加常规元标记。并通过PHP设置到期日 实施例

  <meta http-equiv="expires" content="<?php echo date(whatever type); ?>" />