Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

My JavaScript doesnt always print the correct Last Modified Date on my HTML Web pages. Sometimes it works and sometimes it doesnt where it prints "12/31/1969". I have many pages on Solaris and Windows 2000 servers. I assume I should use server side software instead to get this to work.

Here is what I came up with to fetch last modification date:
my ($ldate) = (head('http://www.mydomain.here/mypage.htm'))[2]; my ($lastdatehere) = scalar localtime($ldate);
Question is this the correct way to do it and how can get this Perl to work in HTML pages? Where would I put it and what do I need to do to get it to work??? I need to get this to work in html pages: mypage.html and anotherpage.html etc...

For what it is worth here is how I have it in JavaScript:
<script language="Javascript">document.writeln("Web page last modified + on " + document.lastModified.bold())</script>

Replies are listed 'Best First'.
Re: Last Mod using server side perl
by Ninthwave (Chaplain) on Dec 31, 2003 at 18:21 UTC

    Non Perl answer:

    Use SSI if it is enabled on your web host. and included the follwing in your html.

    <!--#echo var="LAST_MODIFIED"-->.

    or

    <!--#flastmod file="mypagename.shtml" -->

    From Apache.org and Microsoft

    To do this in perl on static pages you would need ssi enabled and ssi has built in functions to do it so the script would not be needed.

    Update: For clarity the last paragraph should probably best read.
    To do this in perl on static pages you would need ssi enabled. Because ssi has built in functions to do it, you would not need a perl script to create this.

      Still not sure how I would do it. I understand the stat function but how can I get perl to work on a HTML page?
        You either use SSI (in which case better solutions are outlined above) or you have perl print the entire webpage.
Re: Last Mod using server side perl
by pg (Canon) on Dec 31, 2003 at 18:09 UTC

    Your Java Script was doing the right thing. Java Script gets date last modified from the HTTP header, if it presents. In case it does not present, document.lastModified returns "0", which is then interpreted as the "beginning of time". Doesn't matter what script language you use, this cannot be resolved on client side, as the field simply does not present.

    Do it on server side, and make sure your web server does send date last modified whenever it is needed.

    Check out stat() under perlfunc doc, and that should give you the modified date, format it, then just insert it into your HTTP header, and send out.

    Update:

    liz, the behavior is documented, and it does return "0". Happy New Year!

    Happy New Year! ChemBoy.

      ...document.lastModified returns "0", which is then interpreted as the "beginning of time".

      Minor nit: if the date displayed is "12/31/1969", then document.lastModified probably returned "-1", rather than "0". In other words, the second before midnight Jan 1st, 1970, which is the beginning of time as far as *nix is concerned.

      By the way, this nitpicking only feels appropriate because it's only hours away from midnight, Jan 1st, 2004. ;-)

      Liz

        Nit back atcha—for those of us in zones less than GMT (e.g. GMT -400, my current location), Unix date 0 is indeed in 1969, since the beginning if time took place in England. :-)



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

        liz, it is documented behavior to return "0", but not "-1". If you take time zone into consideration, it is definitely not a surprise to see "1969/12/31" get printed.