Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Call a CGI Script from HTML

  1. Jan 20, 2007 #1
    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!
  2. jcsd
  3. Jan 20, 2007 #2


    User Avatar
    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.
  4. Jan 20, 2007 #3
    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 (Text):
    <img src="/path/to/cgi/counter.cgi" />
  5. Jan 21, 2007 #4
    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.
  6. Jan 21, 2007 #5
    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: Jan 21, 2007
  7. Sep 6, 2009 #6
    Thanks, Twist!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook