首页 > 代码库 > ubuntu 下CGI c语言 获取form表单的数据

ubuntu 下CGI c语言 获取form表单的数据

前面文章:使用cgi c 写了一个helloworld  


这次 主要使用CGI c语言 获取form表单的数据

1 login.c

aries@ubuntu:/usr/lib/cgi-bin$ cat login.c
#include<stdio.h>  
#include<stdlib.h>  
#include <string.h>  
int main(){  
    int i,len=0;  
    char poststr[100];  
    char m[10],n[10];  
    char *req_method;     
    printf("Content-type:text/html\n\n");  
    printf("<meta charset=\"utf-8\">");  
    printf("<title>检测页面</title>");  
    printf("<h1>将post过来的和数据库中的信息进行比对即可</h11>");  
    req_method = getenv("REQUEST_METHOD");   
     if (!strcmp(req_method, "POST"))  
       len=atoi(getenv("CONTENT_LENGTH"));  
  
    printf("传过来的字符数: %d \n",len);  
        fgets(poststr,len+1,stdin);  
    printf("显示post过来的数据:<br /><br />");  
    printf("%s\n",&poststr);  
    return 0;  
}  

aries@ubuntu:/usr/lib/cgi-bin$ sudo gcc -o login.cgi login.c

aries@ubuntu:/usr/lib/cgi-bin$ ls
login.c  login.cgi


2 login.html

aries@ubuntu:/var/www$ cat login.html 
<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">密  码</td>
       <td><input type="password" name="Password"></td>
    </tr>
    <tr>
       <td><input type="submit" value=http://www.mamicode.com/"登  录">>


3 http://localhost/login.html





4 点击登录 




获取成功!