24 มิถุนายน 2559

Force clear cache on page เคลียร์แคชหน้าเว็บเพจ

Using HTML Meta Tags

The most basic approach to the prevention of page caching is one that utilizes HTML meta tags:


Using HTTP Headers

A better approach is to use the HTTP protocol itself, with the help of PHP’s header function, to produce the equivalent of the two HTML meta tags above:

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Pragma: no-cache');
?>

We can go one step further than this, using the Cache-Control header that’s supported by HTTP 1.1-capable browsers:

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
?>

Setting a Page Expiry Header

The header that’s easiest to implement is the Expires header–we use it to set a date on which the page will expire, and until that time, web browsers are allowed to use a cached version of the page. Here’s an example of this header at work:

expires.php (excerpt)

function setExpires($expires) {
header(
‘Expires: ‘.gmdate(‘D, d M Y H:i:s’, time()+$expires).’GMT’);
}
setExpires(10);
echo ( ‘This page will self destruct in 10 seconds
’ );
echo ( ‘The GMT is now ‘.gmdate(‘H:i:s’).'
’ );
echo ( ‘View Again
’ );
?>

ไม่มีความคิดเห็น: