Linux CGI get post User Login

Source: Internet
Author: User
CGI is the abbreviation of Common Gateway Interface. It is translated into Chinese and is the Common Gateway Interface. It is the background processing of web pages. Program Run on the server side and can be written in multiple languages. The most common one is Perl (because Perl has powerful string processing functions, and CGI programs often process many strings ). For example, generally, You need to register a forum or email address. You need to enter your username and password. First, you must enter a static page with two text boxes on it, you are required to enter the user name and password, and a button for submission and resetting to submit user input. When the user clicks the submit button, the request is sent to the server, the CGI program on the server parses the user input and verifies whether the user input is valid or not. The following is a simple example, Code Is the best solution:

<HTML>
<Head> <title> User login verification </title> <Body>
<Form name = "form1" Action = "/cgi-bin/output. cgi" method = "Post">
<Table align = "center">
<Tr> <TD align = "center" colspan = "2"> </TD> </tr>
<Tr>
<TD align = "right"> User Name </TD>
<TD> <input type = "text" name = "username"> </TD>
</Tr>
<Tr>
<TD align = "right"> password & nbsp; Code </TD>
<TD> <input type = "password" name = "password"> </TD>
</Tr>
<Tr>
<TD> <input type = "Submit" value = "login"> </TD>
<TD> <input type = "reset" value = "Remove"> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

------------------------- The following is the source code of the CGI processing program ----------------------------

/* 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; charsets = iso-8859-1 \ n ");

Printf ("Content-Type: text/html \ n ");

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

Req_method = getenv ("request_method ");

Input = getcgidata (stdin, req_method );

//What we getInputThe string may be in the following format:

// Username = "admin" & Password = "AAAAA"

//Where"Username ="And"& Password ="All are fixed.

//While"Admin"And"AAAAA"They all change, and we also need to get

//Front9Characters areUsername =

//In"Username ="And"&"Is the username to be obtained.

For (I = 9; I <(INT) strlen (input); I ++)

{

If (input [I] = '&')

{

Name [J] = '\ 0 ';

Break;

}

Name [J ++] = input [I];

}

//Front9Characters+ "& Password =" 10Characters+ UsernameNumber of characters

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

For (I = 19 + 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 static html page above requires the user to enter the user name and password. After the user enters the page, click "Log on" to submit the user's input to the server. CGI to process, here, just as a demo, output. CGI displays user input on the page.

Operation prompt: copy the first paragraph of HTML code to a folder and set it to login.htm. Note that the extension must be HTM or HTML. The second paragraph of code isC LanguageSource codeI have compiled and compiled the file named output.exeunder vcw.+, renamed it output.cgi, and put login.htm in the website directory, and output. cgi In the cgi-bin directory of the website. The pen uses the apacheserver, which stores login.htm under htdocs and output. cgi In the cgi-bin directory. Enter in the browser

Http: // 127.0.0.1/login.htm

It will appear

On this page, when the user enters the user name and password, click "Log on" to display the following page
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 ".
It should be noted that the CGI program should be run on the local machine and a web server supporting CGI should be installed. The most common free web server is Apache, which can be easily downloaded. After the installation, you can configure login.htm in the htdocs directory, put output. cgi In the cgi-bin directory, and start the Web server to explain the CGI program.

Some content cannot be displayed. For more information, see my cublog:
Http://blog.chinaunix.net/u/5391/showart_265981.html

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.