Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CGI with C

Posted on 1998-02-25
6
Medium Priority
?
253 Views
Last Modified: 2013-12-25
I am going to test a CGI-script. I've written two files:
1) test.html
********************************
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Developer Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<!-- Insert HTML here -->
<form method=get action="test.cgi">
Enter some text here:<br>
<input type="text" name="sometext" value="">
<p>
<input type="submit" value="Send">
</form>

</BODY>
</HTML>
********************************

2) test.c
********************************
#include <stdio.h>
#include <stdlib.h>

void header(), footer();

int main()
{
      header();
      printf("Querry String: %s\n\n", getenv("QUERY_STRING"));
      footer();

      return 0;
}

void header()
{
      printf("Content-type: text/html\n\n");
      printf("<html>\n");
      printf("<head>\n<title>Example CGI Output</title>\n</head>\n");
      printf("<body>\n\n");
}

void footer()
{
      printf("</body>\n");
      printf("</html>\n");
}
********************************

I build the c-fiel successed and get "test.exe". I change it to "test.cgi". Then I haven't permit to our server, so I save the files under the same directory.

I open the "test.html" with Netscape browser. I tip some text in the input window. But as I click the "Sene"-button, it shows some strange text which I can't understand.

How can I solve the problem?


chen wei

0
Comment
Question by:chenwei
[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
  • 3
  • 2
6 Comments
 

Author Comment

by:chenwei
ID: 1832032
Edited text of question
0
 

Author Comment

by:chenwei
ID: 1832033
Edited text of question
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1832034
Your program should show something like:

sometext=the_text_you_typed_in

with sequences of "%xx" (where xx is the ASCII code in hexadecimal) replacing any non-printable chars.

If this is what you get, then this is how CGI works.
You need your string parsing routines to exctract and decode key-value pairs from the query string.

BTW, the getenv() function can fail and return NULL, so you should do something like:

--
int main(void) {
  char *query_string = getenv("QUERY_STRING");

  header();

  if(query_string) printf("Query string: %s\n", query_string);
  else puts("Cannot get query string!");

  footer();

  return 0;
}
--//--

Cheers.
0
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 

Author Comment

by:chenwei
ID: 1832035
First I would like to thank julio's answer. But it dosen't help.
I was told (and I am sure) that the files I wrote are correct. The problem may be that I should save the cgi-file(test.cgi) under the directory:/cgi-bin by the server, then the cgi-script can only be executed by server not by client. But I haven't the access to the server.

I do want to test the cgi-scripts I have written, but I don't know where and how.
0
 
LVL 1

Expert Comment

by:Michel021497
ID: 1832036
I think you should ask your provider for a solution for this problem. He can give you access to your cgi-bin directory.
0
 
LVL 5

Accepted Solution

by:
julio011597 earned 50 total points
ID: 1832037
Yes chenwei, CGI is a protocol stating how a web server can communicate with an external program; i.e., no server... no CGI.

To test your scripts locally, you should install a web server on your PC and work with it (you can download a web server for free from both Netscape and Microsoft).

Anyway, as Michel says, you could first try to talk to your Internet Provider and see if they can help you.
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

722 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