本文介绍了如何测试“If-Modified-Since” HTTP头支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP如何准确测试远程网站是否支持If-Modified-SinceHTTP标头。



我已经阅读,如果远程文件您GET自从头请求中指定的日期以来已被修改 - 它应该返回200 OK状态。如果没有被修改,它应该返回304未修改。



因此,我的问题是,如果服务器不支持If-Modified-Since但仍返回200 OK? / p>

有几个工具,检查您的网站是否支持If-Modified-Since,所以我想我问他们是如何工作的。



编辑:



我使用Curl执行了一些测试,发送以下内容:

  curl_setopt($ ch,CURLOPT_HTTPHEADER,array(If-Modified-Since:.gmdate('D,d MYH:i:s \G\M\ T',time()+ 60 * 60 * 60 * 60))); 
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ ch,CURLOPT_MAXREDIRS,5);
curl_setopt($ ch,CURLOPT_HEADER,true);
curl_setopt($ ch,CURLOPT_NOBODY,true);
curl_setopt($ ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ ch,CURLOPT_FORBID_REUSE,true);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,4);
curl_setopt($ ch,CURLOPT_TIMEOUT,4);

未来日期google.com返回;

  HTTP / 1.0 304未修改
日期:Fri,05 Feb 2010 16:11:54 GMT
服务器:gws
X-XSS保护:0
X-Cache:MISS。
Via:1.0。:80(squid)
连接:close

如果我发送;

  curl_setopt($ ch,CURLOPT_HTTPHEADER,array(If-Modified-Since:.gmdate ,d MYH:i:s \G\M\T',time() -  60 * 60 * 60 * 60))); 
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ ch,CURLOPT_MAXREDIRS,5);
curl_setopt($ ch,CURLOPT_HEADER,true);
curl_setopt($ ch,CURLOPT_NOBODY,true);
curl_setopt($ ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ ch,CURLOPT_FORBID_REUSE,true);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,4);
curl_setopt($ ch,CURLOPT_TIMEOUT,4);

过去的日期,google.com返回;

  HTTP / 1.0 200 OK 
日期:Fri,05 Feb 2010 16:09:12 GMT
Expires:-1
Cache-Control:private,max-age = 0
Content-Type:text / html; charset = ISO-8859-1
服务器:gws
X-XSS保护:0
X-Cache:MISS。
Via:1.0。:80(squid)
连接:close

然后我将两者发送到bbc.co.uk(不支持它);



未来的一个返回;

  HTTP / 1.1 200 OK 
日期:Fri,05 Feb 2010 16:12:51 GMT
服务器:Apache
Set-Cookie: BBC-UID = 84bb66bc648318e367bdca3ad1d48cf627005b54f090f211a2182074b4ed92c40ForbSoft%20Web%20Diagnostics%20%28URL%20Validator%29; expires = Tue,04-Feb-14 16:12:51 GMT; path = /; domain = bbc.co.uk;
Accept-Ranges:bytes
Cache-Control:max-age = 0
Expires:Fri,05 Feb 2010 16:12:51 GMT
Pragma:no-cache
Content-Length:111677
Content-Type:text / html

过去的回报;

  HTTP / 1.1 200 OK 
Date:Fri,05 Feb 2010 16:14:01 GMT
服务器:Apache
Set-Cookie:BBC-UID = 841b66ec4424cd91e81e88a014a3c5e50ed4e20c0e07174c4ff59675cd2fa210ForbSoft%20Web%20Diagnostics%20%28URL%20Validator%29; expires = Tue,04-Feb-14 16:14:01 GMT; path = /; domain = bbc.co.uk;
Accept-Ranges:bytes
Cache-Control:max-age = 0
Expires:Fri,05 Feb 2010 16:14:01 GMT
Pragma:no-cache
Content-Length:111672
Content-Type:text / html
解决方案

我已经进行了一些测试,它似乎工作如下:



如果您发送的If-Modified-Since标题的日期是过去的日期(当前时间之前的5分钟),那么网站.com,w3.org,mattcutts.com将返回HTTP / 1.1 304 Not Modified标题。网站如yahoo.com,bbc.co.uk和stackoverflow.com总是返回HTTP / 1.1 200 OK。



Last-Modified标题无需做任何事情与If-Modified-Since,因为发送回HTTP / 1.1 304 Not Modified头的整个点是,你不必发送正文因此,我的问题的答案是,如果一个网站没有返回一个HTTP / 1.1 304不支持当您发送If-Modified-Since 5分钟前标题时,网站不能正确支持If-Modified-Since请求。



编辑:我忘了添加一个好的测试是让对域的正常HEAD请求(例如w3.org),获取上次修改日期,然后用If-Modified-Since:进行另一请求。这将测试支持上次修改值和If-Modified-Since请求。请注意:只是因为服务器发送回上次修改日期并不意味着它支持If-Modified-Since


Using PHP how can I accurately test that a remote website supports the "If-Modified-Since" HTTP header.

From what I have read, if the remote file you GET has been modified since the date specified in the header request - it should return a 200 OK status. If it hasn't been modified, it should return a 304 Not Modified.

Therefore my question is, what if the server doesn't support "If-Modified-Since" but still returns a 200 OK?

There are a few tools out there that check if your website supports "If-Modified-Since" so I guess I'm asking how they work.

Edit:

I have performed some testing using Curl, sending the following;

curl_setopt($ch, CURLOPT_HTTPHEADER, array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T',time()+60*60*60*60)));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);

i.e. a date in the future google.com returns;

HTTP/1.0 304 Not Modified
Date: Fri, 05 Feb 2010 16:11:54 GMT
Server: gws
X-XSS-Protection: 0
X-Cache: MISS from .
Via: 1.0 .:80 (squid)
Connection: close

and if I send;

curl_setopt($ch, CURLOPT_HTTPHEADER, array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T',time()-60*60*60*60)));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);

i.e. a date in the past, google.com returns;

HTTP/1.0 200 OK
Date: Fri, 05 Feb 2010 16:09:12 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Server: gws
X-XSS-Protection: 0
X-Cache: MISS from .
Via: 1.0 .:80 (squid)
Connection: close

If I then send both to bbc.co.uk (which doesn't support it);

The future one returns;

HTTP/1.1 200 OK
Date: Fri, 05 Feb 2010 16:12:51 GMT
Server: Apache
Set-Cookie: BBC-UID=84bb66bc648318e367bdca3ad1d48cf627005b54f090f211a2182074b4ed92c40ForbSoft%20Web%20Diagnostics%20%28URL%20Validator%29; expires=Tue, 04-Feb-14 16:12:51 GMT; path=/; domain=bbc.co.uk;
Accept-Ranges: bytes
Cache-Control: max-age=0
Expires: Fri, 05 Feb 2010 16:12:51 GMT
Pragma: no-cache
Content-Length: 111677
Content-Type: text/html

The date in the past returns;

HTTP/1.1 200 OK
Date: Fri, 05 Feb 2010 16:14:01 GMT
Server: Apache
Set-Cookie: BBC-UID=841b66ec44232cd91e81e88a014a3c5e50ed4e20c0e07174c4ff59675cd2fa210ForbSoft%20Web%20Diagnostics%20%28URL%20Validator%29; expires=Tue, 04-Feb-14 16:14:01 GMT; path=/; domain=bbc.co.uk;
Accept-Ranges: bytes
Cache-Control: max-age=0
Expires: Fri, 05 Feb 2010 16:14:01 GMT
Pragma: no-cache
Content-Length: 111672
Content-Type: text/html

So my question still stands.

解决方案

I have performed some testing on this and it appears to work as follows;

If you send an If-Modified-Since header with a date that is in the past (5 mins previous to the current time should do it) then sites such as google.com, w3.org, mattcutts.com will return a "HTTP/1.1 304 Not Modified" header. Sites such as yahoo.com, bbc.co.uk and stackoverflow.com always return a "HTTP/1.1 200 OK".

The "Last-Modified" header has nothing to do with "If-Modified-Since" because the whole point of sending back a "HTTP/1.1 304 Not Modified" header is that you don't have to send the body with it (thus saving bandwidth - which is the whole point behind this).

Therefore, the answer to my question is that if a site doesn't return a "HTTP/1.1 304 Not Modified" header when you send an "If-Modified-Since 5 mins ago" header, the site doesn't support the "If-Modified-Since" request properly.

If I am incorrect, please say so and provide testing to show.

Edit: I forgot to add that a good test is to make a normal HEAD request to the domain (e.g. w3.org), grab the "Last Modified" date and then make another request with "If-Modified-Since:". This will test that both the "Last Modified" value and "If-Modified-Since" request are supported. Please Note: just because the server sends back a "Last Modified" date doesn't mean it supports "If-Modified-Since"

这篇关于如何测试“If-Modified-Since” HTTP头支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:13