Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:08:21+00:00 2026-06-03T23:08:21+00:00

I think my question seems pretty casual but bear with me as it gets

  • 0

I think my question seems pretty casual but bear with me as it gets interesting (at least for me :)).

Consider a PHP page that its purpose is to read a requested file from filesystem and echo it as the response. Now the question is how to enable cache for this page? The thing to point out is that the files can be pretty huge and enabling the cache is to save the client from downloading the same content again and again.

The ideal strategy would be using the “If-None-Match” request header and “ETag” response header in order to implement a reverse proxy cache system. Even though I know this far, I’m not sure if this is possible or what should I return as response in order to implement this technique!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:08:23+00:00Added an answer on June 3, 2026 at 11:08 pm

    Serving huge or many auxiliary files with PHP is not exactly what it’s made for.

    Instead, look at X-accel for nginx, X-Sendfile for Lighttpd or mod_xsendfile for Apache.

    The initial request gets handled by PHP, but once the download file has been determined it sets a few headers to indicate that the server should handle the file sending, after which the PHP process is freed up to serve something else.

    You can then use the web server to configure the caching for you.

    Static generated content

    If your content is generated from PHP and particularly expensive to create, you could write the output to a local file and apply the above method again.

    If you can’t write to a local file or don’t want to, you can use HTTP response headers to control caching:

    Expires: <absolute date in the future>
    Cache-Control: public, max-age=<relative time in seconds since request>
    

    This will cause clients to cache the page contents until it expires or when a user forces a page reload (e.g. press F5).

    Dynamic generated content

    For dynamic content you want the browser to ping you every time, but only send the page contents if there’s something new. You can accomplish this by setting a few other response headers:

    ETag: <hash of the contents>
    Last-Modified: <absolute date of last contents change>
    

    When the browser pings your script again, they will add the following request headers respectively:

    If-None-Match: <hash of the contents that you sent last time>
    If-Modified-Since: <absolute date of last contents change>
    

    The ETag is mostly used to reduce network traffic as in some cases, to know the contents hash, you first have to calculate it.

    The Last-Modified is the easiest to apply if you have local file caches (files have a modification date). A simple condition makes it work:

    if (!file_exists('cache.txt') || 
        filemtime('cache.txt') > strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
        // update cache file and send back contents as usual (+ cache headers)
    } else {
        header('HTTP/1.0 304 Not modified');
    }
    

    If you can’t do file caches, you can still use ETag to determine whether the contents have changed meanwhile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this question seems a bit complex but I think its all related
This seems like a fairly obvious question, but I haven't been able to think
It seems that others have had variations on this question, but from what I
I know this question seems subjective but it's really pretty simple. As a long
I'm pretty sure that this question already has been answered somewhere. It just seems
This is a super naive question (I know), but I think that it will
I think the question title seems to explain eveything. I want to detect whether
I think my question might seem a bit odd, but here it goes; I'm
I know the questions seems ambiguous, but I couldn't think of any other way
i think this is a simple question but I've searched around and can't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.