Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can somone please help me with C++ CGI?

Posted on 1998-01-16
2
Medium Priority
?
153 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

636 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