How can I call a CGI script from HTML for my webpage counter?

  • Thread starter kingtazie
  • Start date
  • Tags
    Html
In summary, to call a CGI script from HTML for a webpage counter, you need to use the <form> tag and specify the method as "POST". Then, in the action attribute, provide the URL for the CGI script along with any necessary parameters. The CGI script will then be executed and the counter will be updated accordingly on the webpage.
  • #1
kingtazie
2
0
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!
 
Technology news on Phys.org
  • #2
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.
 
  • #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:
<img src="/path/to/cgi/counter.cgi" />
 
  • #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.
 
  • #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:
  • #6
Thanks, Twist!
 

Related to How can I call a CGI script from HTML for my webpage counter?

1. What is a CGI script and how does it work?

A CGI (Common Gateway Interface) script is a program written in a scripting language such as Perl or Python that allows a web server to interact with a user's request. When a user clicks a link or submits a form on a webpage, the web server calls the CGI script to process the request and generate a response that is then sent back to the user's browser.

2. How do I call a CGI script from HTML?

To call a CGI script from HTML, you need to use the <form> tag with the method="POST" attribute and specify the path to the CGI script in the action attribute. You can also use the <a> tag with the href attribute to call a CGI script, but it will be a GET request instead of POST, which may be less secure.

3. Can I use any scripting language for my CGI script?

Yes, you can use any scripting language that is supported by your web server. Some common languages used for CGI scripts include Perl, Python, and PHP. It is important to make sure that your web server is configured to run CGI scripts in the chosen language.

4. How do I pass data from HTML to a CGI script?

You can pass data from HTML to a CGI script by using the <input> tag with the name attribute and the value attribute. The name attribute will be used as the key for the data in the CGI script, and the value attribute will be the corresponding value. You can also use other input elements such as <select> and <textarea>.

5. How do I retrieve data from a CGI script and display it on my webpage?

To retrieve data from a CGI script and display it on your webpage, you can use the <?php echo $variable; ?> syntax if you are using PHP. If you are using another scripting language, you will need to use the appropriate syntax for that language. The CGI script can also generate HTML code that can be displayed on the webpage. You can also use AJAX to retrieve data from the CGI script without refreshing the entire webpage.

Similar threads

  • Programming and Computer Science
Replies
5
Views
492
  • Programming and Computer Science
Replies
2
Views
889
  • Programming and Computer Science
Replies
11
Views
910
  • Programming and Computer Science
Replies
7
Views
664
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
702
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top