简体   繁体   English

PHP Header - 位置重定向似乎丢失了缓存

[英]PHP Header - Location Redirect seems to lose caching

I'm doing some cURL checks on response codes of files before redirecting requests to different hosts (to make sure the files exist before I redirect).在将请求重定向到不同的主机之前,我正在做一些 cURL 检查文件的响应代码(以确保在我重定向之前文件存在)。

When we're all happy, I send them off-server to remote host and we're all happy.当我们都很高兴时,我将它们从服务器发送到远程主机,我们都很高兴。

The only problem seems to be it's not caching on the user's machine.唯一的问题似乎是它没有缓存在用户的机器上。 Is there something I can do to let the user browser know this is the same file?我可以做些什么让用户浏览器知道这是同一个文件吗? I thought it would inherently know as it's just a header location redirect (ie header('location:http://www.locationtogo.com/asda?242');我认为它天生就知道,因为它只是一个 header 位置重定向(即 header('location:http://www.locationtogo.com/asda?242');

Thanks for any help!谢谢你的帮助!

------- UPDATE ---------- - - - - 更新 - - - - -

Seems to be quite a few questions on this;似乎对此有很多问题; so let me explain step by step...所以让我一步一步解释......

  1. User requests file from handler.php用户从 handler.php 请求文件
  2. handler.php does cURL check to see if response code is handler.php 执行 cURL 检查响应代码是否为
  3. if response code is 200 I was redirecting to the file如果响应代码是 200 我正在重定向到文件
  4. however each time reloaded the handler.php for the same file... it would reload the entire file但是每次为同一个文件重新加载 handler.php ......它会重新加载整个文件

I've now realised - stupid mistake - but will list here just incase anyone experiences the same - I was of course, looking for response 200, not 200 or 304.我现在已经意识到 - 愚蠢的错误 - 但会在这里列出以防万一有人遇到同样的情况 - 我当然是在寻找响应 200,而不是 200 或 304。

This is now caching.现在正在缓存。

You need to add a Cache-Control or Expires header along with the redirect to indicate that it can be cached.您需要添加Cache-ControlExpires header 以及重定向以指示它可以被缓存。

Example (this will allow the browser to cache the response for an hour)示例(这将允许浏览器将响应缓存一小时)

header('Cache-Control: max-age=3600')

PHP and apache will not cache a PHP file by default. PHP 和 apache 默认不会缓存 PHP 文件。 I recently ran into this myself.我最近自己遇到了这个。 You can however return a 304 if you want and that will fix the problem.但是,您可以根据需要返回 304,这将解决问题。 I did it with etags.我用etags做到了。

Simply send the browser the first time an etag hash the first time it visits it.只需在第一次访问浏览器时发送一个 etag hash 即可。

header('Etag: '.$eTagHash);

Then, check to see if the etag is there, if so tell the browser it's a 304 not modified.然后,检查 etag 是否存在,如果存在则告诉浏览器它是 304 未修改。

if(isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
    if(trim($_SERVER['HTTP_IF_NONE_MATCH']) == trim($eTagHash)){
       header("HTTP/1.1 304 Not Modified");
       exit;
    }}

You can set the $eTagHash to anything you want.您可以将 $eTagHash 设置为您想要的任何内容。 You should have etags enabled on the apache server as well (most do).您也应该在 apache 服务器上启用 etags(大多数都这样做)。

If you ever need to redirect somewhere else, just change the etag hash and when it doesnt match you can send the browser the header etag again with the new value.如果您需要重定向到其他地方,只需更改 etag hash 并且当它不匹配时,您可以使用新值再次向浏览器发送 header etag。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM