我有一个每天发送不同图像的 php 脚本。我希望在白天缓存图像,但我的脚本没有按预期工作,因为在每个请求中都返回 200 OK 。
代码是:
<?php
$uno = '1.jpg';
$dos = '2.jpg';
$tres = '3.jpg';
$cuatro = '4.jpg';
$cinco = '5.jpg';
$seis = '6.jpg';
$siete = '7.jpg';
$today=date(l);
header('Content-Disposition: inline');
header('Content-Type: image/jpg');
header("Content-Transfer-Encoding: Binary");
$expire=60*60*24*1; // seconds, minutes, hours, days
header('Pragma: public');
header('Cache-Control: maxage='.$expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// Find what today is? using date function
if($today==Monday){
readfile($uno);
exit;
}
elseif($today==Tuesday){
readfile($dos);
exit;
}
elseif($today==Wednesday){
readfile($tres);
exit;
}
elseif($today==Thursday){
readfile($cuatro);
exit;
}
elseif($today==Friday){
readfile($cinco);
exit;
}
elseif($today==Saturday){
readfile($seis);
exit;
}
elseif($today==Sunday){
readfile($siete);
exit;
}
?>
这有什么问题?
更新:
我刚刚尝试按照WebnetMobile.com的说法重构代码,但似乎缓存问题仍然存在,因为它仍然返回 200 OK。
新代码是:
<?php
$today=date("l");
header('Content-Disposition: inline');
header('Content-Type: image/jpg');
header("Content-Transfer-Encoding: Binary");
$expire=60*60*24*1;// seconds, minutes, hours, days
header('Pragma: public');
header('Cache-Control: maxage='.$expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// Find what today is? using date function
$map = array( 'Monday' => '1.jpg',
'Tuesday' => '2.jpg',
'Wednesday' => '3.jpg',
'Thursday' => '4.jpg',
'Friday' => '5.jpg',
'Saturday' => '6.jpg',
'Sunday' => '7.jpg'
);
readfile( $map[ $today ] );
?>