Solved

Can somone please help me with C++ CGI?

Posted on 1998-01-16
2
146 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 100 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

776 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