C language CGI instance __c language

Source: Internet
Author: User
Tags int size strcmp

CGI is the abbreviation of Common Gateway Interface, translated into Chinese is a common Gateway interface, it is a Web page background processing program, running on the server, can be written in a variety of languages, the most common is Perl (because Perl has a powerful string processing function, CGI programs often have to handle a lot of strings. For example, generally speaking, forums or mailboxes need to be registered, users need to enter a username and password, first give you a static page, there are two text boxes, asking you to enter a username and password, there is a Submit and reset button, for submitting user input, when the user clicks the Submit button, This request is sent to the server side, the CGI program on the server will parse the user's input, and verify that the user's input is legitimate, whether it is validated and so on. Here's a very simple example, and the code is the best explanation:

<body>
<form name= "Form1" action= "/cgi-bin/output.cgi" method= "POST" >
<table align= "center" >
&LT;TR&GT;&LT;TD align= "center" colspan= "2" ></td></tr>
<tr>
&LT;TD align= "Right" > Username </td>
<td><input type= "text" name= "Username" ></td>
</tr>
<tr>
&LT;TD align= "Right" > Secret &nbsp;&nbsp; code </td>
<td><input type= "password" name= "password" ></td>
</tr>
<tr>
<td><input type= "Submit" value= "Login" ></td>
<td><input type= "reset" value= "Cancel" ></td>
</tr>
</table>
</form>
</body>

-------------------------The following is the source code for the CGI handler----------------------------

* OUTPUT.C * *

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* Getcgidata (file* fp, char* Requestmethod);

int main ()

{

Char *input;

Char *req_method;

Char name[64];

Char pass[64];

int i = 0;

int j = 0;

printf ("Content-type:text/plain; charset=iso-8859-1/n/n ");

printf ("content-type:text/html/n/n");

printf ("The following is query reuslt:<br><br>");

Req_method = getenv ("Request_method");

input = Getcgidata (stdin, Req_method);

The input string we obtained might look like the following form

Username= "admin" &password= "AAAAA"

where "username=" and "&password=" are fixed.

and "admin" and "aaaaa" are changes, but also we want to obtain

The front 9 characters are username=

Between "Username=" and "&" is the username we're going to take out.

for (i = 9; i < (int) strlen (input); i++)

{

if (input[i] = = ' & ')

{

NAME[J] = '/0 ';

Break

}

Name[j++] = Input[i];

}

Previous 9 characters + "&password=" 10 characters + username number of characters

We don't want it, so we omit it, not copy it.

for (i = + strlen (name), j = 0; i < (int) strlen (input); i++)

{

Pass[j++] = Input[i];

}

PASS[J] = '/0 ';

printf ("Your Username is%s<br>your Password is%s<br>/n", name, pass);

return 0;

}

char* Getcgidata (file* fp, char* Requestmethod)

{

char* input;

int Len;

int size = 1024;

int i = 0;

if (!strcmp (Requestmethod, "get")

{

input = getenv ("query_string");

return input;

}

else if (!strcmp (Requestmethod, "POST"))

{

Len = atoi (getenv ("content_length"));

input = (char*) malloc (sizeof (char) * (size + 1));

if (len = = 0)

{

Input[0] = '/0 ';

return input;

}

while (1)

{

Input[i] = (char) fgetc (FP);

if (i = = size)

{

INPUT[I+1] = '/0 ';

return input;

}

--len;

if (feof (fp) | | (! (len)))

{

i++;

Input[i] = '/0 ';

return input;

}

i++;

}

}

return NULL;

}

The first above is a static HTML page that requires the user to enter the username and password, and when the user clicks on the "Login" button, the input is submitted to the server. Handled by output.cgi, here, just as a demo, output.cgi the user's input on the page.

Operation Tip: Copy the first paragraph of HTML code to a text file, save as login.htm, note that the extension to use HTM or HTML, the second section of code is C language source code, the author vc++6.0 under the compiler, Generate the file for Output.exe, rename it to output.cgi,login.htm on the site's directory, output.cgi place it on the site's Cgi-bin directory. The author uses the Apache server, will login.htm placed under the Htdocs, will output.cgi placed in the Cgi-bin directory. In the browser input
Http://127.0.0.1/login.htm
will appear

User name
Password

This page, when the user enters the username and password, and clicks the "Login" button, the following page appears
The following is query REUSLT:

Your Username is admin
Your Password is admin888

Here we assume that "admin" is entered in the User Name text box and "admin888" is entered in the Password box.
You need to explain that you want to run the CGI program on this computer, you need to install a CGI-enabled Web server, and the most common free Web server is Apache, which is easy to download. After installation, basically do not need to do any configuration, put login.htm under the Htdocs directory, put output.cgi in Cgi-bin directory, start the Web server, you can explain the CGI program.

I also just contact, write very rough, if there is discomfort, look at the vast numbers of netizens criticized.

http://blogold.chinaunix.net/u/5391/showart_265981.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.