Forum Moderators: phranque

Message Too Old, No Replies

On the fly content regeneration with PHP?

On the fly content regeneration with PHP

         

jlanier01

2:14 pm on Apr 15, 2004 (gmt 0)



I wanted to know if the following effect be achieved with php as the application instead of the .cgi engine?
I appreciate any links, suggestions, or sample code you may have.

Thanks,
Jason

On-the-fly Content-Regeneration
Problem Description:

Here comes a really esoteric feature: Dynamically generated but statically served pages, i.e. pages should be delivered as pur static pages (read from the filesystem and just passed through), but they have to be generated dynamically by the webserver if missing. This way you can have CGI-generated pages which are statically unless one (or a cronjob) removes the static contents. Then the contents gets refreshed.

Problem Solution:

This is done via the following ruleset:

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond ^page\.html$ page.cgi [T=application/x-httpd-cgi,L]

Here a request to page.html leads to a internal run of a corresponding page.cgi if page.html is still missing or has filesize null. The trick here is that page.cgi is a usual CGI script which (additionally to its STDOUT) writes its output to the file page.html. Once it was run, the server sends out the data of page.html. When the webmaster wants to force a refresh the contents, he just removes page.html (usually done by a cronjob

pete_m

2:43 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



Hi Jason

Yeah, this can absolutely be done in php. One method would be to cache the entire page (by default, php writes stuff to the client ASAP). At the end of the code execution, you can then write the cache to a file and then write it to the client.

For more info, see the php website. You're looking at

ob_start()

to start the page caching, and then

ob_getcontents()

which returns the cached data as a string. Write this to the file and then

ob_end_flush()

to send send the data to th
e client.