Welcome to WebmasterWorld Guest from 174.129.151.95

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

running php function AFTER page is delivered

keeping pages static but allow tracking, etc

   
3:37 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One of my websites is composed of all static pages.

I've got a php include that I have inserted at the end of the pages to do tracking and other housekeeping, but it doesn't output anything to the page itself.

Unfortunately this still forces me to put all the pages through the php interpretor.

Is there any clever way I can use apache (possibly a module in htaccess?) to simply execute the php script AFTER any html page is delivered to a user, therefore keeping their access speedy and a side benefit of truly keeping the pages static, complete with etags, content-length and "last modified"?

Thanks for any ideas!

3:47 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



Running your static pages thru php shouldn't slow it down.
4:03 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a slight overhead, and that can add up for lots of simultanous views.

On an server with a high load, why make a static html page require any more cpu attention than it really needs?

4:07 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



This is just an idea. Not sure if it would be effective or make any difference...

plain HTML
plain HTML
plain HTML
plain HTML
plain HTML
plain HTML
plain HTML
plain HTML

<?php
// flush to the browser
flush();
// now do all your time-consuming PHP stuff
do_infinite_loop($forever);
?>

4:08 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah I was thinking about the flush command that I had vaguely remembered seeing.

Is there also a command to forcefully close the connection so the server is completely done with the visitor before the code begins the other housekeeping?

4:14 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there any clever way I can use apache (possibly a module in htaccess?) to simply execute the php script AFTER any html page is delivered to a user

I might be taking you literally but page is not delivered until all its components were delivered, ie: images, css etc. If you want to run PHP function after that all happens then you can just insert web bug, which would call PHP script that will do some work (log?) and then return 30-40 bytes of invisible GIF.

If however you meant to do some work BEFORE html file is fully delivered then you can use methods described above.

4:20 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oooh that's an interesting thought I had forgotten about, a pseudo image that runs as the php code as an element of the page.

Many third party tracking programs use that technique but since this is my own code I could make it work better.

I can make the image name include the source page name so that it doesn't rely on referers which can be blocked.

But what about folks who block certain kinds of images and the problem of image caching. I know I can make the php script always look new to the browser but sometimes they cache or block the image anyway. Hmnm....

4:26 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But what about folks who block certain kinds of images and the problem of image caching.

Beat caching by specifying unique name for the image on every page, or append?datestamp to URL - it won't affect nothing. This image can be used for legitimate purpose - some web designers use invisible images and stretch them to create filled rectangles (can't find better term).

So long as its not a 3rd party web bug, and especially functional element of a page it should not be blocked. Word of warning though - referer will be set to that of page for the image request, so you won't get original referer.

5:09 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



Are you trying to log something that Apache can't already log for you?

[httpd.apache.org...]

5:13 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



Ideally, something like this would work:

apache config:
<Files *.html>
AddOutputFilter PHP .html
AddInputFilter PHP .html
</Files>
php_value auto_append_file /path/logger.php

logger.php:
<? error_log("hello, world");?>


But unfortunately not only does this send the whole page through PHP for parsing, but PHP also removes the Last-Modified etc. that static serving sets.

You can do this easily with Perl and mod_perl though.

You can attach a function to run after the content-sending phase. Apache will do everything just like it normally does for static serving- no overhead added- and run your function after the connection is closed. (PerlLogHandler)

It's very, very fast, but you will need to spend some time managing your RAM usage.

5:10 am on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very interesting jollymcfats, exactly what I hoped some apache guru would know.

Even thought it's not perfect, it's a great start for research.

Many thanks!