?
Solved

Can somone please help me with C++ CGI?

Posted on 1998-01-16
2
Medium Priority
?
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

777 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