duanmu5641 2014-06-17 08:01
浏览 71
已采纳

PHP检查浏览器缓存中是否存在url

My web project is based on XSLT created XHTML pages which takes some time to be created. On the other side, the original XML changes rarely. Therefore, I set a browser cache with a HTTP header in PHP:

header("Cache-Control: public, max-age=3600"); 

Going "back" and "forward" in the browser is now very smooth.

But there is another case: By clicking on some links the user could possibly get the very same page again. The URL is also exactly the same. But in this case the page isn't loaded from the browser cache, but created anew. I've found a very nice solution for a server cache on http://www.webgeekly.com/tutorials/php/learn-how-to-cache-content-with-php-in-under-5-minutes/ . But retrieving the file from the browser cache would even be faster.

So, is there a way in PHP to do this:

  • check whether a certain URL part (like "output.php?id_of_search=123454&action=select") is in the browser cache
  • if yes: Load the page with that URL from the browser cache.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • duanmei1694 2014-06-17 08:27
    关注

    If your php-script is executed the browser already decided not to use its cache. Otherwise it would not request your page at all and just serve the user from cache. Meaning: There is no way to trigger the cache at that time from php.

    The reason why it is not using the cache, is because the browser thinks the result will be another page. So if you make sure the same page has always the same URL linking to it, the cache will work fine.

    One pitfall here might be

    http://example.com/somepage.php
    http://example.com/somepage.php?someparam=1
    http://example.com/somepage.php?someparam=2
    

    These URL all generate the same response, but the browser thinks they are different.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?