Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    482
    Thanks
    97
    Thanked 17 Times in 17 Posts

    Can a web page tell a browser to perform a hard refresh?

    Each week I upload GIF files into a folder on my ftp site. There may be 10 of them, and they are always named exactly the same as the previous week, therefore they overwrite the old ones.

    More often than not, I need to refresh the browser for the updated images to show. I can't expect my site visitors to understand that they need to perform a hard refresh.

    Is there any html code that I can place in my page that will tell a revisiting browser that it should perform a hard refresh? My first instinct is that this would really be controlled by the individual in their browser settings, and my answer is no. I just want to ask to make sure.

    --Kevin

    .
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well.Building a web page is like building a birdhouse. Put it up there and watch 'em come. | Maintaining the aspect ratio of an image is more important than having a cold orange pop.

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,315
    Thanks
    29
    Thanked 279 Times in 273 Posts
    Quote Originally Posted by Kevin_M_Schafer View Post
    Is there any html code that I can place in my page that will tell a revisiting browser that it should perform a hard refresh?
    Presumably, this problem could be ameliorated by sending out a Last-Modified HTTP header with the images. I'm not quite sure how you do that though.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    482
    Thanks
    97
    Thanked 17 Times in 17 Posts
    Hi Arbitrator,

    Thanks for the reply. I'm going to read up on what you mentioned. The images are really GIF files of my newspaper pages. By reusing the file names each week, I don't have to open the actual web page and perform any coding. It's simply drag and drop. It's slick, but it could be hours before a revisiting browser displays on its own my updated content/images.

    --Kevin

    .
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well.Building a web page is like building a birdhouse. Put it up there and watch 'em come. | Maintaining the aspect ratio of an image is more important than having a cold orange pop.

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,305
    Thanks
    11
    Thanked 283 Times in 282 Posts
    Slightly different from what you asked, and possibly wrong, but might generate some interesting googling. I thought I remembered someone solving this problem via something like:

    <img src='myimage.gif?id=12345'>

    Where the ID was a randomly generated number (ie, probably created in PHP or perhaps JS?), which caused the URI to be different on each browser load and therefore not found in the cache. So you end up with no cache, rather than a weekly cache, though you might add the smarts to it like:

    <img src='myimage.gif?id=ww12'>

    and only change the id weekly.

    I've never fiddled with this, but it might be worth a try if you have the time.

    Dave

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    The way tracknut suggested is the way I do it. I just have php echo out a random string of numbers

    <img src="image.gif?<?php echo rand(1,999999999); ?>" alt=""/>

  • Users who have thanked Nightfire for this post:

    Kevin_M_Schafer (02-14-2012)

  • #6
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    482
    Thanks
    97
    Thanked 17 Times in 17 Posts
    Thank you Dave and Nightfire,

    Okay, this is great. So what I need to do is open the code and instead of my standard image tags, implement what Nighfire has shown? Is this all there is to it, or do I need an additional php script?

    --Kevin

    .
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well.Building a web page is like building a birdhouse. Put it up there and watch 'em come. | Maintaining the aspect ratio of an image is more important than having a cold orange pop.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Kevin_M_Schafer View Post
    Is this all there is to it, or do I need an additional php script?
    That's all you'd need to force a reload each and every time the image is referenced.

    If you just want to force the reload once a week then work out what offset you need to swap the change of week number on the server to alighn with when you upload the new images and just update the current date/time by that offset and then grab the week number and add that instead of the random number.

    The other alternative as Arbitrator mentioned is to set a Last_modified header. The easiest way to do that would be to serve the images via a PHP script that reads in the image and sends it to the browser after adding the appropriate headers. You'd then reverse the src around on the img tag so it calls the PHP script passing the image as the querystring.

    The following code posted in the PHP manual shows how to send headers from PHP to identify if the given file has been modified since the cached copy.

    Code:
    <?php 
     $last_modified_time = filemtime($file); 
    $etag = md5_file($file); 
    
    header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); 
    header("Etag: $etag"); 
    
     if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || 
         trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
         header("HTTP/1.1 304 Not Modified"); 
         exit; 
     }
    You'd then need one more header to identify the file as a gif image:

    Code:
    header('Content-Type: image/gif');
    and code to transfer the content of the actual image (note you should also validate the $_GET value is one of your images before running any of the code.

    Code:
    readfile($_GET['img']);
    Last edited by felgall; 02-13-2012 at 07:46 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    Kevin_M_Schafer (02-14-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •