How to do an If-Modified-Since request

Discussion in 'PHP' started by AHA7, Oct 6, 2007.

  1. #1
    Hello,

    How can I do an If-Modified-Since request with PHP to see if a webpage has been recently changed or not?
     
    AHA7, Oct 6, 2007 IP
  2. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
    
    // file to check (with path)
    $filename = "filename.php";
    
    // what date you want to check against (ie July 1, 2007):
    $date_check = mktime(0, 0, 0, 7, 1, 2007);
    
    $mod_date = date("Y-m-d h:i",filemtime($filename));
    $date_since = date("Y-m-d h:i",$date_check);
    
    if ($mod_date > $date_since)
    {
        //whatever you wanna do here, e.g.
        echo $filename . " has been modified since: " . $date_since;
    }
    else { echo $filename . " has not been modified since: " . $date_since; }
    
    ?> 
    
    
    PHP:
     
    james_r, Oct 6, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Just to clarify, do you want to get the date of a local or remote file?

    If you want to get the remote date, have a look at this thread.
    http://forums.digitalpoint.com/showthread.php?t=326547

    And james_r, there is no need to convert the timestamps into a date format. You can directly compare the timestamps.
     
    nico_swd, Oct 7, 2007 IP
  4. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    good catch nico, i had done the conversion to easily display in the echo statement.. but it really wasn't necessary, could have done it just once in the echo.
     
    james_r, Oct 7, 2007 IP