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

  • Home
  • SEARCH
  • 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 9223839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:10:11+00:00 2026-06-18T04:10:11+00:00

The following code is what I am using in my image.php (image.jpeg too under

  • 0

The following code is what I am using in my image.php (image.jpeg too under htaccess). I want the user to be able to access a cached copy if the source hasn’t been changed (thus it is the same image). However if the source has been changed get the new copy. This image is a background image thus caching is important. Is this code correct? I tried it in Chrome and refreshing the page always reloaded the image. Going to the page again (clicking enter) always kept the cached image even when it had been updated.

<?php
session_start(); 
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
if(file_exists('settings.xml')){
        $xml = simplexml_load_file('settings.xml');
        define("BACKGROUND_IMAGE", $xml->background->image);
        define("BACKGROUND_TIME", $xml->background->time); // when image changes this is set to time()
    }
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
       && 
  (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == BACKGROUND_TIME)) {
  // send the last mod time of the file back
  header('Last-Modified: '.gmdate('D, d M Y H:i:s', BACKGROUND_TIME).' GMT', 
  true, 304);
  exit;
}
// open the file in a binary mode
$name = BACKGROUND_IMAGE;
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
  • 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-18T04:10:13+00:00Added an answer on June 18, 2026 at 4:10 am

    Personally I use something like this and works perfectly;

    $etag = '"'. md5($img) .'"';
    if (isset($_SERVER['HTTP_IF_NONE_MATCH']) 
           && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
        header('HTTP/1.1 304 Not Modified');
        header('Content-Length: 0');
        exit;
    }
    
    $expiry = 604800; // (60*60*24*7)
    header('ETag: '. $etag);
    header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
    header('Expires:'.        gmdate('D, d M Y H:i:s', time() + $expiry) .' GMT');
    ...
    // show/send/read image
    

    But here is something else if you like to see (ref: Answering HTTP_IF_MODIFIED_SINCE and HTTP_IF_NONE_MATCH in PHP).

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

Sidebar

Related Questions

My HTML page displays an image with the following code (using Flickr) <?php echo
i have to change image color dynamically using php i used the following code
I use the Following Php Code to Manipulate image using GD library and upload
I am using following code to get source for image in wpf (image is
I am using following code for add button with image on tool bar, but
I am using following code to load an image from internal memory to tab
I am using following code to give round corners to the image placed on
In LibGdx, texture image is stored in asset folder and loaded using following code.
I'm using the following code to load an image and associated image map: <span><%=
I'm using the following code to rotate an image http://www.platinumball.net/blog/2010/01/31/iphone-uiimage-rotation-and-scaling/ that's one of the

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.