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

本文介绍了一种使用CGI C语言实现的表单数据处理方法。通过编写C语言脚本,可以有效地从HTML表单中获取POST提交的数据,并将其显示在网页上。此过程涉及环境变量的使用、标准输入读取及简单的数据打印。

前面文章:使用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="登  录"></td>
       <td><input type="reset" value="取  消"></td>
    </tr>
</table>
</form>
</body>
</html>


3 http://localhost/login.html





4 点击登录 




获取成功!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值