Solved

Can somone please help me with C++ CGI?

Posted on 1998-01-16
2
144 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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now