Every so often, people ask me how I call SWI-Prolog from PHP.
So I've put up a copy of the script, as the page you're
reading now. Most of it is just static HTML. The active part
is the call to Prolog, between the PHP <?
and ?>
tags in the middle of the current
page. This call
is as follows:
system("nice -n15 /software/bin/pl -L128k -G128k -T128k -A128k -f /home/popx/spin/spin.pl -g spin,halt")Here, the
nice
reduces the priority of
the process. This reduces the load on my Internet Service Provider's
machine. Doing so is not essential...; but it's polite.
The /software/bin/pl
invokes Prolog: that's
where it lives, on my ISP's machine.
The -L128k -G128k -T128k -A128k
set Prolog
memory limits. "Spin" is a small program, so doesn't need
much memory: reducing the amount needed also reduces load.
The -f /home/popx/spin/spin.pl
makes Prolog
consult the specified file, which is my Spin
source code. And -g spin,halt
causes it
to execute the spin
predicate defined therein; and then halt.
Spin sends its output to the current output stream, which
the PHP system
call will send back as
server output.
If you want to know how to preserve state over sessions, take a look at Traveller.
The text of the script follows:
<HTML> <HEAD> <TITLE>SF story generator </HEAD> <BODY BGCOLOR=#ffffff> [ <A HREF="/index.html">Jocelyn Ireson-Paine's Home Page</A> | <A HREF="/spin.html">SF plot-generator source</A> | <A HREF="/spin_script.html">SF plot-generator PHP script</A> ] <P> <CENTER> <H1>SF plot generator </CENTER> <P> Reloading this page will randomly generate the plot of a simple science-fiction story. (There is an Excel/Excelsior version of this generator at <A HREF="http://www.j-paine.org/excelsior/repository/spin/index.html">www.j-paine.org/excelsior/repository/spin/index.html</A>; and you can also try the Traveller game at <A HREF="http://www.j-paine.org/cgi-bin/traveller.php">www.j-paine.org/cgi-bin/traveller.php</A>.) </P> <P> <TABLE BORDER> <TR> <TD BGCOLOR=#FFCCFF> <B> <? system("nice -n15 /software/bin/pl -L128k -G128k -T128k -A128k -f /home/popx/spin/spin.pl -g spin,halt") ?> </B> </TD> </TR> </TABLE> </P> <P> The program is one I've used in teaching Prolog. Its content is based on "The Science Fiction Horror Movie Pocket Computer" by Gahan Wilson, from "The Year's Best Science Fiction No. 5", edited by Harry Harrison and Brian Aldiss, Sphere, London, 1972. The source is <A HREF="/spin.html">here</A>; it's running under <A HREF="http://www.swi-prolog.org/">SWI-Prolog</A> and <A HREF="http://www.php.net/">PHP</A>. If you want to know how to call SWI-Prolog from PHP, you can see my script <A HREF="/spin_script.html">here</A>. </P> <P> <ADDRESS>31st October 2008</ADDRESS> <P> </BODY> </HTML>
31st October 2008