Solved

Page not properly refreshing after image upload

Posted on 2003-11-02
7
300 Views
Last Modified: 2008-02-26
Hello!!!!!!!!

I have a page which has five boxes (image spot 1, 2, 3, 4 and 5). In these boxes is where your images will be displayed if you have uploaded any.  Now, my php code and html form is on the same page.  If for example you haven't uploaded any files yet (clean profile) and you upload an image to spot 1, when the page is displayed again, you see the image in spot 1. If you upload *again* to spot 1, the upload actually worked but the old image is still displayed because it's in the browser's cache. If I press F5, then the new image is displayed.

I tried fixing it by redirecting to the same page after the upload but it didn't really do anything. Here's a snippet:

-----------------------------------------------------------------------------------------------
if(!(copy($_FILES['imagefile']['tmp_name'], "profiles/".substr($var__username,0,1)."/".$var__username."/" . $newfilename . "_big.jpg")))
{
      $ul__errorNumber = 1;
      $ul__errorMessage = "Unexpected error occured. Please try again.";
}
else
{
      $dst_w = 160;
      $dst_h = 120;
      $dst_img = imagecreatetruecolor($dst_w,$dst_h);
      imagecopyresized($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
      imagejpeg($dst_img, "profiles/".substr($var__username,0,1)."/".$var__username."/" . $newfilename . ".jpg", 90);

      $dst_w = 92;
      $dst_h = 69;
      $dst_img = imagecreatetruecolor($dst_w,$dst_h);
      imagecopyresized($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
      imagejpeg($dst_img, "profiles/".substr($var__username,0,1)."/".$var__username."/t_" . $newfilename . ".jpg", 90);

      $result = mysql_query("UPDATE tbl_personals_profile SET      profile_mainpic = 1 WHERE profile_userID = $var__userID");
      header("Location: images.php");
}
-----------------------------------------------------------------------------------------------

It's not the first time I've had problems with cache. I've done a few ASP sites and some people didn't even see the new updated content on the site.  Am I doing something wrong? Am I suppose to be using a meta tag that has the date of the page or something to check for new content?

Anyway.. I'm clueless..  hehe..  if you have any ideas - please lemme know!!!

Thanks!
0
Comment
Question by:bloodtrain
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:red010knight
ID: 9668792
If the page is REALLY light on images (preferrably none of more than 5k each and no more than 10 images) then I would suggest setting the file to not be cached. I don't remember the code off hand nor do I have access to it. But its supposed to be rather easy... I found this on the web though some say there is a bug in some IE versions that may make this not work... but Hopefully it will now...

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");

Good Luck and Happy coding!
Red010Knight
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 9669014
Using the headers to tell browsers to reload the page doesn't seem to work all the time.

Sometimes the browser will cache it regardless of the headers you send.  One way to fix this is to force the URL to change every time by adding a random GET parameter to the URL like so:

<form action="whatever.php?random=<? echo rand(); ?>">
...
</form>

This way, the browser will always reload it since it will see a distinct URL (unless you get the same rand() value twice in a row).

You can rename the "random" parameter to whatever you like, and ignore it in your scripts.  It's a bit of an ugly way to do it, but it should work.
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9669134
If that's the case, a good random would be md5(time()) this way the chances of getting the same number are virtually impossible. As you will never have the same time twice... though its only good on a unix system. If your not on unix you may want to look into JS function that gets the current time or soemthing like that.

Good luck with your code,
Red010Knight
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Author Comment

by:bloodtrain
ID: 9672097
I tried both methods mentioned and still no luck. I read a little about using the headers and apparently they work for IE only over a secure connection (https).

The trick with using a random number didn't work either.

I just noticed that if I upload a new pic, my old pic that is cached, is shown in all pages until I hit F5. It's weird - real weird.

I'm using PHP 4.3.2 on Windows XP pro as a development server and my other server is running Redhat 9. Same results on both servers.

You know, you reach a level at one point that you just wanna smash something..  ;)

0
 
LVL 3

Accepted Solution

by:
wide_awake earned 50 total points
ID: 9674974
The same random parameter trick probably needs to be applied to the URLs in your img src="blah" part.

It's annoying that browsers want to cache images, but it probably saves the internet a lot of load overall.

It's also a realy stupid way to fix it...

for example, you'd have this:

<img src="http://www.experts-exchange.com/images/woman_in_specticals.gif?blah=1200"></img>

instead of

<img src="http://www.experts-exchange.com/images/woman_in_specticals.gif"></img>
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9675875
Well I hate to suggest this, but you could always try a javascript image loader.

Basically you would have files somewhere and you would put in js syntax - I don't have the script on hand sorry -

function imageLoader(){

document.image1.src="imagesrc";
document.image2.src="imagesrc";
document.image3.src="imagesrc";

}

in the body tag:
<body onload="javascript:imageLoader();">

in the document
<image name="image1" src="">
<image name="image2" src="">
<image name="image3" src="">

------
its a dumb way to do it and it should do the job as javascript doesn't care what is cached it just does what it is told.

Sorry I couldn't be more code specific, but its been a while since I had to actually write out a script from memory on syntax.

Red010Knight
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 9676465
Thanks for your help!  I looked at some sites similar to mine and I noticed that's exactly what they were doing.  :)

Now I can work on my next problem!  hehe

0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now