Example: Expires Headers in Scripts
This Perl CGI script shows one way to use Expires header in scripts, for demonstration purposes.
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Util; use Date::Parse; my $q = new CGI; print $q->header(-type => "text/html", -status => 200, -expires => "+60s"); print "<html><body>\n<h1>Expires Test</h1>\n" . "<p>Date: ".scalar localtime()."</p>\n" . "<table>\n"; foreach my $var (sort keys %ENV) { next unless $var =~ /^HTTP_/; print "<tr><td>$var</td><td>$ENV{$var}</td></tr>\n"; } print "</table></body></html>\n"; exit 0;
When you load the script through a CGI web server, it will display a response like this:
Expires Test
Date: Fri Aug 3 17:02:08 2007
HTTP_ACCEPT | text/xml,application/xml,application/xhtml+xml,... |
HTTP_ACCEPT_CHARSET | ISO-8859-1,utf-8;q=0.7,*;q=0.7 |
HTTP_ACCEPT_ENCODING | gzip,deflate |
HTTP_ACCEPT_LANGUAGE | en-us,en;q=0.5 |
HTTP_CACHE_CONTROL | max-age=0 |
HTTP_CONNECTION | keep-alive |
HTTP_COOKIE | 200 |
HTTP_HOST | localhost |
HTTP_KEEP_ALIVE | 300 |
HTTP_USER_AGENT | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.5) Gecko/20060601 Firefox/2.0.0.5 (Ubuntu-edgy) |
The script sends a response with an Expires header 60 seconds in the future, using the CGI module's built-in feature to calculate an HTTP-compatible date from the string "+60s", which means "now plus 60 seconds".
After loading the script, if you put the caret back into the address bar and press Enter again to load the page again, within 60 seconds, no request will be sent and the Date will not change. However, if you press the Refresh or Reload button in your browser, the page will be reloaded.
After 60 seconds, the page will expire from the cache, and reloading it. Using either method will result in a new request and a fresh date.