how to clear cache in PHP

Discussion in 'PHP' started by rafiqasad, Aug 8, 2007.

  1. #1
    Hello all.
    I want to know how to clear cache in PHP. I am updating a image from admin panel. But after uploading old images displays. When i refresh the page then the new image dispaly. Can any one solve this problem. Thanks in advance
     
    rafiqasad, Aug 8, 2007 IP
  2. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #2
    Its got nothing to do with PHP. Its the browser that store image cache and rightly replaces it with the new image when you refresh ;) .
    So you don't need to worry at all :)
     
    killerj, Aug 8, 2007 IP
  3. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The cache is on the client side not the server side, so this can't be done in PHP. A solution might be to put a query string and a ever changing value after the source of the image like:

    echo '<img src="'.$imageSource.'?'.gmdate('U').'" />';
    Code (markup):
     
    norfstar, Aug 8, 2007 IP
  4. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #4
    Yes that could be done , but then , it would also mean you ask an extra variable to get processed for a not-so-big-deal :rolleyes:

    Therefore , use it only if its absolutely necessary ;) . I dont think it is though :)
    Good luck with your website.
     
    killerj, Aug 8, 2007 IP
  5. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Unless the server is very very slow, it's going to take well under a millisecond to process that, and actually having the new image show is the only deal asked for.
     
    norfstar, Aug 8, 2007 IP
  6. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #6
    I was assuming that there are tons of other images as well . So that would mean tons of other variables .
    oh well . . .different people . Different opinions . . . :)
     
    killerj, Aug 8, 2007 IP
  7. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If there are multiple images, you could use:

    $now = gmdate('U');
    
    echo'<img src="'.$imageSource1.'?'.$now.'" />
    <img src="'.$imageSource2.'?'.$now.'" />
    <img src="'.$imageSource3.'?'.$now.'" />
    <img src="'.$imageSource4.'?'.$now.'" />
    <img src="'.$imageSource5.'?'.$now.'" />
    <img src="'.$imageSource6.'?'.$now.'" />
    ';
    Code (markup):
    Or if the image sources are in an array

    $imageSource = array('1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg');
    
    $now = gmdate('U');
    
    foreach ($imageSource as $var){
       echo'<img src="'.$var.'?'.$now.'" />';
    }
    Code (markup):
    meaning you'd only have to generate a fresh string to put on the query string once. If generating it by using gmdate() (or a similar date/time function) proves to resource intensive, rand() could be used instead to generate a random number which if your browser hasn't cached before will cause it to load the new image :).
     
    norfstar, Aug 8, 2007 IP
  8. rafiqasad

    rafiqasad Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    Thanks a lot brother yes this function work for me.
     
    rafiqasad, Aug 8, 2007 IP
  9. fastbuilder

    fastbuilder Peon

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes, nice script. I have been looking for this also..
     
    fastbuilder, Aug 9, 2007 IP