Solved

CGI with C

Posted on 1998-02-25
6
249 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

626 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