[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Can somone please help me with C++ CGI?

Posted on 1998-01-16
2
Medium Priority
?
155 Views
Last Modified: 2013-12-25
Is there somone who can help me learn CGI using C++?  I am just not getting it.  If you have a few minutes, could you explain where I put the text/html and all the other stuff so I can get my CGI script to run?  I need to make two:  one to send me a form, and the other to do a password check to get to another HTML page.  Please email me or answer this question to help.  My email is:  Topace@geocities.com
0
Comment
Question by:Topace
2 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 400 total points
ID: 1831836
When your CGI program (regardless of whether it is written in PERL, C, or whatever) is running, the web server hooks up the cgi program's stdin and stdout to the client browser.  So anything you write out of your C program using a printf() call will end up at the browser.  (This is not 100% correct for the purists who might read this, but for your purposes it is.)  So the rist thing you normally write to the client is:

printf("Content-type: text/html\n\n");

Don't forget the 2 "\n\n" at the end of this line (I have a habit of doing it!  So a minimum response cgi program might be:

#include <stdio.h>

void main()
{
  printf("Content-type: text/html\n\n");
  printf("<HTML>\n");
  printf("Hello World\n");
  printf("</HTML>\n");
}

Note that you can run this from the command line and direct the output to a file, and then open the file with your browser.  This is really helpful for debugging HTML formatting problems.

The next step is accepting input from the client.  The simplest is using the METHOD=GET (as opposed to POST) in your form action.  Using GET, the data is passed to the cgi program through environment variables.  The one you are interested in is called QUERY_STRING.  You get it like this:

char *qs;
qs = getenv("QUERY_STRING");

Now if qs is a NULL, you didn't get anything from the user and you should do some error processing.  So the following code will echo the user's response back:

#include <stdio.h>
#include <stdlib.h>

void main()
{
  char *qs;

  qs = getenv("QUERY_STRING");

  printf("Content-type: text/html\n\n");
  printf("<HTML>\n");
  if(qs == NULL){
    printf("ERROR, no QUERY was received\n");
  }
  else{
    printf("QUERY: %s\n", qs);
  }

  printf("</HTML>\n");
}

Now you need to write some code to parse the QUERY_STRING as all of the form fields are passed as one long string pasted together with &, spaces are replaced with +.  The general form is:

arg1="data+1"&arg2="data+2"


0
 
LVL 1

Author Comment

by:Topace
ID: 1831837
Thank you very much.  I am starting to get the hang of this now.  Could you answer me one more question?

Since I am using Borland Turbo C++ LITE, do I use cout instead of printf?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question