Disable Browser Cache in Cakephp

When developing web apps, sometimes it is better to disable browser cache. We can do it in php by following code

header('Cache-Control: no-store, private, no-cache, must-revalidate');     // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false);  // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');                  // Date in the past  
header('Expires: 0', false); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header ('Pragma: no-cache');

So how do we set these in cakephp? Very simple

$this->disableCache();

This will produce headers like this

Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: [current datetime] GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache

I recommend calling this function in certain function rather than calling it beforeFilter() function in app_controller.php

About Zakir Hyder

This entry was written by .

22. July 2011 by Zakir Hyder
Categories: CakePHP, PHP, Web Development | Tags: , , , , , , | Comments

Comments

  1. […] Disable Browser Cache in Cakephp When developing web apps, sometimes it is better to disable browser cache. We can do it in php by following code So how do we set these in cakephp? Very simple This will produce headers like this I recommend calling this function in certain function rather than calling it beforeFilter() function in app_controller.php […]