Call a CGI Script from HTML

  • Thread starter kingtazie
  • Start date
2
0

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!
 

Answers and Replies

verty
Homework Helper
2,157
198
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.
 
343
1
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" />
 
221
0
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:
1
0
Thanks, Twist!
 

Related Threads for: Call a CGI Script from HTML

  • Last Post
Replies
7
Views
3K
Replies
17
Views
4K
Replies
1
Views
2K
Replies
1
Views
16K
  • Last Post
Replies
2
Views
1K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
7
Views
2K
Top