test php disabled caching

夙愿已清 提交于 2019-12-24 16:53:32

问题


My site had a problem in that certain browsers (especially opera and gecko) were "over-caching" (caching far too much for my taste).

I've just added the following PHP snippet to hopefully disable caching in all browsers:

<?php 
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

Question: How would you test this out, to make sure it actually works?

Additions, Notes: I need a stupidly simple test; my skill level is pretty low. I can't do cache busting (url changes) because it's a one-page site.


回答1:


Get the current time() and store it in the PHP session. Use var_dump() to print out the $_SESSION array, and also to print the current time(). Click "refresh" a few times. Expect the session array to remain stable and the current time to change. If both remain stable, the script output is being cached.

Example here: http://www.laprbass.com/RAY_cache_test.php

<?php // RAY_cache_test.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// SEND HEADERS
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// START SESSION
session_start();

// GET CURRENT TIME
$now = date('c');

// IF NO TIME IN SESSION YET, STORE ONE
if (!isset($_SESSION['myTest'])) $_SESSION['myTest'] = $now;

// SHOW THE SESSION AND THE CURRENT TIME
echo 'SESSION TIME: ' . $_SESSION['myTest'];
echo '<br/>';
echo 'CURRENT TIME: ' . $now;



回答2:


Most of the browser extensions like Developer Tools for Chrome or Firebug for Firefox allow you to inspect request and response headers which contain all the information you need about caching.

Additionally if you are developing on Windows I can recommend using Fiddler, an excellent debugging proxy which aside for providing deep traffic analysis of your application can explain to you what is going on with caching in plain simple English.




回答3:


You should see the page accesses in your web server log. Compare the access pattern, when the pages are re-requested with and without your additional headers.



来源:https://stackoverflow.com/questions/13923655/test-php-disabled-caching

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!