linux CGI GET POST 使用者登入

來源:互聯網
上載者:User
CGI是Common Gateway Interface的縮寫,翻譯成中文就是通用閘道介面,它是網頁的幕後處理程式,運行在伺服器端上,可以用多種語言書寫,最常用的就是Perl(因為 Perl有強大的字串處理功能,而CGI程式經常要處理許多的字串)。舉個例子來說,通常一般的論壇或郵箱等都需要註冊,需要使用者輸入使用者名稱和密碼, 首先給你一個靜態頁面,上面有兩個文字框,要求你輸入使用者名稱和密碼,還有一個提交和重設的按鈕,用於提交使用者的輸入,當使用者點擊提交按鈕時,這個請求就 被發送到伺服器端,伺服器上的CGI程式就會解析使用者的輸入,並且驗證使用者的輸入是否合法,是否被通過驗證等。下面看一個非常簡單的例子,代碼是最好的解 說:

<html>
<head><title>使用者登陸驗證</title></head>
<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">使用者名稱</td>
       <td><input type="text" name="Username"></td>
    </tr>
    <tr>
       <td align="right">密&nbsp;&nbsp;碼</td>
       <td><input type="password" name="Password"></td>
    </tr>
    <tr>
       <td><input type="submit" value="登 錄"></td>
       <td><input type="reset" value="取 消"></td>
    </tr>
</table>
</form>
</body>
</html>

-------------------------以下是cgi處理常式的源碼----------------------------

/* 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);

       // 我們擷取的input字串可能像如下的形式

       // Username="admin"&Password="aaaaa"

       // 其中"Username="和"&Password="都是固定的

       // 而"admin"和"aaaaa"都是變化的,也是我們要擷取的

      

       // 前面9個字元是UserName=

       // 在"UserName="和"&"之間的是我們要取出來的使用者名稱

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

       {

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

              {

                     name[j] = '\0';

                     break;

              }                  

              name[j++] = input[i];

       }

       // 前面9個字元 + "&Password="10個字元 + Username的字元數

       // 是我們不要的,故省略掉,不拷貝

       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;

}

    上面的第一個是一個靜態html頁面,要求使用者輸入使用者名稱和密碼,當戶輸入之後,點擊“登入”按鈕,則使用者的輸入就被提交到伺服器,由output.cgi來處理,在這裡,只是作為一個示範,output.cgi把使用者的輸入顯示在頁面上。

    操作提示:將第一段HTML代碼拷貝到一個文字檔,另存新檔login.htm,注意副檔名要用htm或者html,第二段代碼是c語言原始碼,筆者在 VC++6.0下編譯通過,組建檔案為output.exe,將其改名為output.cgi,login.htm放在網站的目錄,output.cgi 放在網站的cgi-bin目錄。筆者用的是Apache伺服器,將login.htm放在了htdocs下面,將output.cgi放在了cgi- bin目錄。在瀏覽器輸入

http://127.0.0.1/login.htm

就會出現

這樣頁面,當使用者輸入了使用者名稱和密碼之後,點擊“登入”按鈕,就會出現下面的頁面
The following is query reuslt:

Your Username is admin
Your Password is admin888

這裡我們假設在使用者名稱文字框裡輸入的是"admin",在密碼框裡輸入的是"admin888"。
需 要說明是要在本機上運行該cgi程式,需要裝支援cgi的Web伺服器,最常見的免費Web伺服器就是apache,這個很容易下載到。安裝之後,基本不 需要做任何的配置,把login.htm放在htdocs目錄下面,把output.cgi放在cgi-bin目錄下,啟動web伺服器後,就可以解釋 cgi程式了。

部分內容無法顯示,原文參見我的cublog:
http://blog.chinaunix.net/u/5391/showart_265981.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.