Adding PHP code at the top of an upstreamed page with
Radio UserLand is quite interesting for managing web sites. For example, scriptdigital.com is a PHP web site edited in Radio UserLand that use the built-in ftp driver. The PHP tags
<? ?> doesn't conflict with the Radio tags
<% %> and this open to a lot of interesting applications. Radio UserLand offer me a nice framework for doing my site, especially the hierarchical templates, #directives, and maybe, above all, I don't have to encode my accented characters in HTML entities. Plus, I can do all my work in my beloved BBEdit.
Some functions in PHP can work only if there are at the very top of the page, before the first
<html> tag. An example of one of these functions is
<? session_start ( ) ?> <html> ... </html>
The problem is to get the PHP code before the opening
<html> tag. This could be done probably in a #template.txt (I didn't try though), but its not convenient because you might not want to have this piece of PHP code on each of your page that are included thru this template. Another solution could be to use a macro inserted at the top of the template, and depending on some directives, you could ask this macro to put a piece of PHP code. But I don't find this very convenient. Another solution could be to write your own driver, but again, this might be overkill and also not convenient.
My solution is to use a callback at user.radio.callbacks.upstreamGetUpstreamText. Unfortunately, there is some problems with this callbacks. Basically, you want to get the text after it have been processed by Radio (so you can put your PHP code before the
<html> tag), but to do that, you have to use
radio.upstream.getUpstreamText. And since
user.radio.callbacks.upstreamGetUpstreamText, you ends up by recursing in an infinite loop and Radio crash. UserLand is aware of this situation but didn't provide yet a fix for that. So, what I did, is to paste in my
addPHPcode callback a modified version of
radio.upstream.getUpstreamText that doesn't call any callbacks. If you want more detail on the problems with
radio.upstream.getUpstreamText, please, follow this thread (worth noting that
#autoparagraphs directive mentioned in this thread have been fixed).
Here's how you can use the
addPHPcode callback. Put at the top of your file this directive:
And then, put the PHP code between these two comments (there is no spaces between the opening and closing tags, and the comments have to be uppercase).
<!--PHP_CODE_START--> <? session_start () ?> <!--PHP_CODE_END-->
So in the end, the file on your machine could look like this:
#title "Radio UserLand addPHPcode callback" #autoparagraphs "false" #addPHPcode "true" <!--PHP_CODE_START--> <? session_start () ?> <!--PHP_CODE_END--> <h2>Radio UserLand addPHPcode callback</h2> ...
And the file upstreamed on the server before it get serve by PHP will look like this:
<? session_start ( ); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title> Radio UserLand addPHPcode callback </title> ...
First release: Version 1.0 - December 5 2002
Tested on OS X 10.1.5 with Radio UserLand 8.0.8