Only load the page if it is different from the cached version.

I need to include an HTML page (generated by ASP.net) in a PHP page.

For this, I use:

echo file_get_contents("http://example.com"); 

But in this way, my server should load the page every time my page opens.

I would like to add a cache system, but I need to update the cache every time the contents of example.com change.
What is the best method (if any) to determine if content changes without loading every time the entire page?

Here is the HTTP header of the remote page:

 HTTP/1.1 200 OK => Cache-Control => no-cache Pragma => no-cache Content-Length => 63648 Content-Type => text/html; charset=utf-8 Expires => -1 Server => Microsoft-IIS/7.5 Set-Cookie => ASP.NET_SessionId=xxxxxxxxxxxxxxxx; path=/; HttpOnly X-Powered-By => ASP.NET X-AspNet-Version => 4.0.30319 X-UA-Compatible => chrome=1 X-CID => 2-18 Date => Thu, 12 Sep 2013 08:54:59 GMT Connection => close 

Another site gives me the following:

 Server Response HTTP/1.1 200 OK HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 65367 Content-Type: text/html; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 Set-Cookie: ARRSID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/;Domain=.example.com Set-Cookie: ASP.NET_SessionId=xxxxxxxxxxxxxxxxxxx; path=/; HttpOnly X-Powered-By: UrlRewriter.NET 2.0.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET X-UA-Compatible: chrome=1 X-Powered-By: ARR/2.5 X-Powered-By: ASP.NET X-CID: 1-18 Date: Thu, 12 Sep 2013 08:56:03 GMT 
+4
source share
4 answers

I used the @Prasanth solution, but it was just a comment, and I cannot ask an answer, so I am writing it here. If he wants to write an answer, I will put it as a solution.

For caching on your server see this. knowing when the source changed, you would need to find something unique about the server. does the server have a content-length header like? If so, you may know the page is being updated if this value has changed. But if the site is not completely at your disposal, or you cannot know exactly when the page changed, you will want to update the file cached on your server from time to time, perhaps using the cron job. Edit: Also check if the server has a Last-Modified header, as Ruben said.

Thus, checking the content-length does the trick.

+1
source

Assuming your server supports it, the best way is to use the headers of the specified page.

In particular, check out If-Modified-Since , which does exactly what you need if your web server supports it.

Alternatively, you can check the ETags header, which will provide a content identifier. Changes to the page should change the identifier (usually the timestamp of the page generation is used). Again, this depends on the server configuration.

+3
source

You can use cURL to extract the headers, reload the file, or maintain your cached version depending on the value

 Last-Modified: Fri, 14 Sep 2012 21:51:00 GMT 

heading

+3
source

int filemtime ( string $filename ) returns you the last change date - if it was AFTER your caching time - you can reload the page if you don't get it from the cache.

-4
source

Source: https://habr.com/ru/post/1501797/


All Articles