How can browser caching be utilized to reduce load times when including headers and menus on a webpage with PHP?
Browser caching can be utilized to reduce load times by storing static resources such as headers and menus on the user's browser. This way, these resources do not need to be re-downloaded each time the page is visited, reducing load times significantly.
<?php
// Set the cache expiration time to 1 week
$expires = 60 * 60 * 24 * 7;
header('Pragma: public');
header('Cache-Control: max-age=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
?>
Keywords
Related Questions
- How can PHP be used to write output back into an HTML input or textarea element?
- How can the issue of output before the header() function be resolved in PHP?
- How important is it for PHP developers to regularly update their software and plugins to prevent known vulnerabilities from being exploited by attackers?