+ -
当前位置:首页 → 问答吧 → 求高手用C做一个简单的CGI登录操作

求高手用C做一个简单的CGI登录操作

时间:2010-05-21

来源:互联网

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE2 {font-size: xx-large}
-->
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="login.cgi">
<table width="456" height="245" border="1">
  <tr>
  <td width="446" bgcolor="#ffffcc"><span class="STYLE2">登陆系统</span></td>
  </tr>
  <tr>
  <td bgcolor="#ffffcc">用户名:
  <label>
  <input name="username" type="text" id="username" />
  </label></td>
  </tr>
<tr>
  <td bgcolor="#ffffcc">密 码:&nbsp;
  <label>
  <input name="password" type="password" id="password" />
  </label></td>
  </tr>
  <tr>
<td height="70" bgcolor="#ffffcc"><label>
  <input type="submit" name="Submit" value="login" />
  &nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;  
  <input name="close" type="submit" id="close" onclick="window.close()" value="close" />
 </label></td>
 </tr>
</table>
</form>
</body>
</html>
这个是写的登录界面我不知道写其中的login.cgi用C怎么写
我写了一个如下:#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
char name, password;
printf(“Content-type: text/html\n”);
printf(“\n”);
printf(“<html><head><title>CGI TEST</title></head>”);
printf(“<body>”);
printf(“<H3>Login</H3>”);
if(name==”root”&&password==”root”)
printf(“<P>登陆成功”); 
printf(“<a href =”index.html”></a>”); 
else 
printf(“<P>用户名或密码不正确,请重试”); 
printf(“</body></html>”);
return 0; 
}

但是那个字符串判断有错误用root root登录总是跳转到错误页面:"用户名或密码不正确,请重试"

我这个只要求有一个用户可以登录就可以了 不需要连接数据库那么麻烦请哪位大大指点一下

作者: cyrlzx   发布时间: 2010-05-21

你使用的post方法,数据的传递有cgi的一套机制,不是直接在cgi程序中就可以用的。

对于post方法:你可以通过利用环境变量,在标准输入流中获取form表单中得到的数据。

作者: xiaoshun123   发布时间: 2010-05-22

在网上搜索cgic,这个是用c语言来写的一个关于cgi第三方库,许多cgi的操作都封装在里面了,还挺好用的。

作者: nintendo_dskay   发布时间: 2010-05-26

C的字符串比较函数不是那样的,得用数组保存,然后调用,strcmp(a,'root')来判断。

作者: yu103   发布时间: 2010-05-27

post数据也是可以看到的。
用http的Authorization机制吧,通过验证的用户明会放在服务器端环境变量REMOTE_USER里。

然后,写一个login_check.cgi,网页打开之后,先用JavaScript调用login_check.cgi来查看服务器那边REMOTE_USER里用户名的一些属性,让cgi带回来~~~

作者: piedgogo   发布时间: 2010-05-31

method可以是post和get两种方式,自己可以换了试试,看地址栏显示的不同。

在cgi端,需要根据method的不同,把浏览器传过去的数据解码。

我用的解码函数是从火鸟BBS bbs2www-2.01.tar.gz提取出来的cgi.c和cgi.h,你找来看下就 明白了
或者搜cgi编程,也有介绍。

作者: piedgogo   发布时间: 2010-05-31