Strawberry Prolog CGI Scripts
(ONLY for the Professional edition)
 

If you want to change one static web site and to put some active content in it then you need CGI Script . Here are some typical applications of CGI Scripts , which you can see in our site made as Strawberry Prolog CGI Scripts (you can find the source of these applications in the professional edition in Prolog CGI folder):

If you like to put a registration form.
If you like to recognize your users (by cookie) and to call them by name in their own language.
If you like to visualize some automatically changeable information (like the number of the registered users)
If you like to make search in a database (of registered users for example).
If you like to put a game in your site and to give the possibility to everyone to play it through the web.
If you like to make a chat room in your site.

It is impossible to make a good site without using this technology. Of course, you can use CGI Scripts which are not made specially for your site (like counters of the page visitors). Also you can pay to a professional programmer to write the CGI Scripts which you need. All this is possible but our advice will be to make alone all the functionality which you need as Strawberry Prolog CGI Scripts . This will be an easy and pleasant work. Another possibility is to use Perl but it is easier to study Prolog than Perl and it is much easier to write a program on Prolog than on Perl.

Here is your Strawberry Prolog CGI Scripts guide which will help you to make the functionality for your site. First you need to understand what is CGI Script and how it works.

A CGI Script is a program which resides on the Web Server usually in cgi-bin directory and when the script is invoked e.g. the user has made a request which contains a CGI executable (for example http://host/cgi-bin/myCGI.pro) then the Web Server executes the program and connects its output to the user's input, so everything that the CGI will print on its standard output will be sent to the user.

Simple CGI Script :

?- set_content_type("text/html"),
 write("Hello New User").

First predicate sets the content type of the page to "text/html" and in this way notifies the browser that the type of the following content will be text or html and the browser will parse it if necessary and will show it to the corresponding user. The second predicate prints into the standard output "Hello New User" so the browser will receive a page containing only that string (the result will be the same if you create a text file containing only the string " Hello New User " and try to view it through a web browser).

SUMMARY: To make a web page viewable through a Strawberry PrologCGI Scriptyou should create a file ( myCGI.pro ) and place it into the cgi-bin directory of your Web Server that contains the following:

#!./strawberry

 ?- set_content_type("text/html"),
 write("
 <put your page source here>
 ").

The first row of the file tells the operating system that it should pass the file as an argument to the Strawberry Prolog compiler. Page source can be in text or in HTML format. This text will be the output of your CGI Script . Of course, in this simple example the page source is static and this active page will look the same as the passive one which contains only the Page source.

IMPORTANT: In your "page source " you should replace every quotation mark with double quotation mark and every percentage mark with double percentage mark ( " -> "" and % -> %% )
 

Passing arguments to a CGI Script

Usually when you call CGI Script then you pass some arguments to it. For example you may want to call one CGI Script with different arguments. You can put these arguments just after the name of the program separated with a question mark. For example:

http://host/cgi-bin/myCGI.pro?name=John&password=test

in this way your CGI will receive the arguments' name with a value of "John" and password with value " test ". To get the parameters, you should use the getenv and get_html_form functions:

Query is getenv("QUERY_STRING"),
Name is get_html_form(_, "name", Query).

The Query variable will contain " name=John&password=test " and the Name variable will contain "John".

There are two ways to pass an argument to a CGI Script. The first which we described above is the GET method. In this method the arguments are visible in the address and their length is restricted up to 256 symbols. The second method for passing arguments is the POST method. By this method the fields in the HTML forms are passed.

When you submit a form then the content of the fields is passed as arguments to the CGI Script which is attached to this form. To get the arguments from a POSTmethod use the following:

Query is getenv(post),

If there are two fields in your form with the names: name and password then and if the user (web visitor) fills in these fields John and test respectively then the CGI Script which is attached to this form will receive query " name=John&password=test " and that will be the value which will receive the Query variable.
 

Converting a Strawberry Prolog program into a CGI Script

Suppose you have written a Strawberry Prologgame and want to place it on a Web Server, in this way many people can see it and play it. The first thing you should do is to make the layout (the " look and feel ", the HTML page) of your game. To achieve this you can use your favorite HTML Editor. Let's take for example the Tick-Tack-Toe game (take a look on it in our site). It is 3x3 table, you put " X " and the computer puts " O ", whoever makes three in a row, column or a diagonal wins. The game opens a new window with nine squares on a table. So the first thing you have to think of is to create a table (with your HTML Editor) and to put nine (blank) images in it. (Maybe you will need some GIF images too.) The next step is to remove all predicates (from the Windows game) which open (manipulate) windows, listen for the mouse click etc. You have to leave only the core of the game (the part that chooses the right move according to the position). As a Windows game your program was multi-step program (you play and the game receives your move and then it plays its next move). To make it run as a CGI Script you have to make it a single-step program (each time you have to pass the current position of the game to the CGI program together with your move and it has to return the next situation). You can keep the current position in a list for example [[f,f,f], [f,f,f], [f,f,f]] - nine empty squares, or [[x,o,x], [f,f,f], [f,f,f]] - X,O,X on the first row. You can pass this list each time you make a new move:

http://some/host/cgi-bin/Tick-Tack-Toe.pro?pos=[[f,f,f],[f,f,f],[f,f,f]]&x=1&y=2

In this way you will tell the program what is the current position (empty table) and what is your move. The program should examine the position, to choose the right move and return the HTML page to the user. According to the new position (achieved after the move) the CGI should change the images on the new HTML page. This means that it should return your HTML page but modified. In the new HTML some of the blank images should be changed with images of X and O. Also in this new HTML each blank image has to have a link to the same CGI Script with arguments the new current position and the coordinates of the cell.

In this way the game goes on. When you press on one blank image then you say to the CGI Script what is the current position and what is your move. CGI Script starts, calculates its answer, sends it to you as a HTML page. After answering the CGI Script stops and forgets about you. Of course, you can cheat the program by changing the position in the URL to [[x,x,x],[f,f,f],[f,f,f]] - which will mean that you win! That is because this CGI Script doesn't know who are you and it believes the information which you send to it. It can be done in a different way and to remember the current position but in this case it needs to support a database of all users playing the game at the moment (they can be a lot) and to recognize them (by cookie or by their IP addresses).

Your CGI program should be something like the following:

#!/path/to/the/cgi-bin/strawberry

?-
 Query is getenv("QUERY_STRING"),
 Pos is get_html_form(_, "pos", Query),
 X is scan(get_html_form(_, "x", Query)),
 Y is scan(get_html_form(_, "y", Query)),
 List is scan(Pos),
 choose(NewPos, List, X, Y),
 view(NewPos).

choose(NewPos, OldPos, X, Y) :-
 <some game logic here> .

view(NewPos) :-
 set_content_type("text/html"),
 write("
 <your HTML page source here modified by NewPos >
 ").
 

Tracing Prolog CGI Scripts

CGI Scripts are programs which run on the server and that makes their tracing difficult. Strawberry Prolog gives you the opportunity to create and trace Prolog CGI Scripts in the Strawberry Environment and to install them on your sever only after they are ready.

When you start a CGI Script in the Strawberry Environment then it prints your web page in the Output window in HTML format. If you know HTML format that can be enough to see if your program works correctly or not. An easier way to see the result of the program is to look on it through your web browser.

How to do this?

 1. Save Output window as HTML file (Press Save As when Output is active. Give html extension to the new file).
 2. Open with your web browser the file that you have just created.

Now you can start tracing.
 1. Clear all in the Output window (Select all and delete).
 2. Run your program.
 3. Delete the Yes which is at the end of the Output and eventually Compiling the file ... which is in the beginning of it.
 4. Save the Output file
 5. Go in your web browser and press the refresh button.

In this way you can easily trace your CGI Script . Anyway you can have some problems with the paths of the images which are included in the resulting HTML page. This paths can force you to make small changes to your program before to put it on the server. One possible decision to avoid this problem is to make two folders on your hard disk (and the same on your server). First folder called cgi-bin and the second one called Images . Save the Output in the first folder and put the images which you need in the second one. In this case the path for images will be: "../Images/".

See also:
url_encode