VB icon

Web Page Cache Control

Email
Submitted on: 1/1/2015 11:21:00 AM
By: Patrick Ingle (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: PHP 4.0
Views: 481
 
     Is your PHP/HTML output being cached on the client? My output was especially during development and I got tired of always pressing the browser reload/refresh button. Therefore, I discovered using the PHP header function and the HTML keyword Expires prevents my browser from caching the resultant HTML output. See the code below. Very simple and easy to include and will ensure that your clients will always invoke the server-side PHP script each the page is accessed.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Web Page Cache Control
// Description:Is your PHP/HTML output being cached on the client? My output was especially during development and I got tired of always pressing the browser reload/refresh button. 
Therefore, I discovered using the PHP header function and the HTML keyword Expires prevents my browser from caching the resultant HTML output.
See the code below. Very simple and easy to include and will ensure that your clients will always invoke the server-side PHP script each the page is accessed.
// By: Patrick Ingle (from psc cd)
//
// Inputs:N/A
//
// Returns:A formatted HTML output
//
// Assumes:See purpose above
//
// Side Effects:None known
//**************************************

<?php
	// This script is one way to prevent/control caching of a
	// web page.
	//
	// Example of What Happens without an expires:
	// Your web page is saved to your local hard drive and if
	// you want to reload it, you must click the Refresh/Reload
	// button on the browser. If you just press Return while the
	// URL line has focus, the cache copy is only loaded.
	// This is probably fine for normal pages which do not change
	// but is a headache when developing pages and constantly pressing
	// the Reload/Refresh browser button.
	//
	// Using the Expires and setting the date and time to the current
	// instantenous time, will prevent caching and permit the reload
	// of the page from server just by pressing return while the URL
	// address line is in focus.
	//
	// Another purpose is when you have dynamic content or for PHP
	// when you are referencing a database table. Good pratice would
	// be to expire that page immediately so the next time the client
	// loads the page, any new data is retrieved without any extra
	// effort on the user.
	//
	// IMPORTANT:
	// The Expires: time stamp must be GMT. PHP has a function to
	// format the date in GMT.
	$nowGMT = gmdate("M d Y H:i:s");
	// This is header information and needs to be in the
	// header portion of the client page.
	// If displaying other than graphics, the Content-type
	// needs updating to reflect the content being displayed.
	$header = "Content-type: text/plain\nExpires: $nowGMT\n\n";
	header($header);
	// NOTE:
	// I've discovered, I needed to include the HTML <body> tags
	// when I use the PHP header() function, otherwise the
	// HTML tags are not interpreted properly and are displayed
	// as normal text.
	echo "<body>\n";
	echo "<b>$nowGMT</b>";
	echo "</body>\n";
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.