# Call a CGI Script from HTML

## Main Question or Discussion Point

Hi everyone,

Is it possible to call a CGI program from an HTML page and if so, how is it done? I'm in the process of designing a page and I am working on designing my own counter. Thanks!

Related Programming and Computer Science News on Phys.org
verty
Homework Helper
You would need to modify the apache httpd.conf or whatever to add a target or something like that, I'm sure there is information available out there.

Since HTML isn't a programming language, you technically don't 'call' CGI programs.

provided your web server is configured to execute CGI, all you need to do is reference the CGI program in some fashion.

for example, a counter program that returns an image might be included in an HTML document using something like
Code:
<img src="/path/to/cgi/counter.cgi" />

Yes, do what imabug said.

If you don't want it to be called through an image, make it an <iframe> with no height or width, or set the "display" attribute of the image to "none".

If you want it to communicate asynchronously, you could implement a variant of AJAX, where you change the src of your invisible frame through javascript, dump the .innerHTML attribute into a string, parse it, and change the src of the frame again accordingly.

Simple and effective.

If you are under Apache, there's an even easier way to do it: use SSI (Server Side Includes). Most servers now require the host page be named with a ".shtml" (the 's' is for 'server parsed') before parsing a page, but it works like gangbusters. You don't even need to use an image, you an emit the count as pure text.

The syntax looks like this:

<!--#include virtual="script_name.ext"-->

Just put that link in your HTML page (with the .shtml extension), and whatever text the script "script_name.ext" spits out will show in the HTML page as if it had always been there.

The script needs to set the MIME type to "text/html", even though it is just going back to the server for inclusion before being sent to the client. In PERL, you'd do that with

print "Content-Type: text/html\n\n";

Then just print your counter (after reading from a file, updating the file, etc.), and you're done.

NOTE: your server (even if Apache) may not be configured for SSI. I'm pretty sure the default is for SSI to be on, but only applied to .shtml extensions. The default used to be to parse all .html (or .htm) files, but that chews up a lot of processor time parsing pages with no includes.

Last edited:
Thanks, Twist!