嵌入式ARM+SQLite3+BOA服务器用户登录验证功能

xiarilove 2012-04-05

近期做了嵌入式用户登录验证功能的一个系统,所以把具体实现分享一下:
  开发平台:sqlite3数据库、BOA服务器、CGI
  主要代码:
  网页代码:
<html><meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<head><title>用户登陆验证</title></head>
<body>
<form name="form1" action="/cgi-bin/pass.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>
CGI代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h" 
char* getcgidata(FILE* fp, char* requestmethod);
int main()
{
             char *input;
             char *req_method;
             char namein[12];
             char pass[12];
             char passtemp[12];
             int i = 0;
             int j = 0;
            
             printf("Content-type: text/html\n\n");
        
             req_method = getenv("REQUEST_METHOD");
             input = getcgidata(stdin, req_method);
             // 我们获取的input字符串可能像如下的形式
             // Username="admin"&Password="aaaaa"
             // 其中"Username="和"&Password="都是固定的
             // 而"admin"和"aaaaa"都是变化的,也是我们要获取的
            
             // 前面9个字符是Usernamein=
             // 在"Usernamein="和"&"之间的是我们要取出来的用户名
             for ( i = 9; i < (int)strlen(input); i++ )
             {
                            if ( input[i] == '&' )
                            {
                                         namein[j] = '\0';
                                         break;
                            }                                    
                            namein[j++] = input[i];
             }
         // 前面9个字符 + "&Password="10个字符 + Usernamein的字符数
             // 是我们不要的,故省略掉,不拷贝
             for ( i = 19 + strlen(namein), j = 0; i < (int)strlen(input); i++ )
             {
                            pass[j++] = input[i];
             }
             pass[j] = '\0';

sqlite3_stmt*    stmt;
    
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("/usr/local/boa/cgi-bin/login.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件

//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
//char *sql = " CREATE TABLE user(ID INTEGER PRIMARY KEY,Name text,Passwd text);" ;
//sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
    
//查询数据int ret=0;
int t=0;
sql = "SELECT Passwd FROM user WHERE Name=?";
ret = sqlite3_prepare(db,sql,strlen(sql),&stmt,NULL);
sqlite3_bind_text(stmt, 1, namein,strlen(namein),SQLITE_STATIC);
while( sqlite3_step(stmt) == SQLITE_ROW){
strcpy(passtemp,sqlite3_column_text(stmt,0));
if(strcmp(passtemp,pass)==0)
    { printf("<html>\n") ;
        printf("<head><title>welcome</title></head>\n") ;
        printf("<body>\n") ;
        printf("<h1>welcome home!!</h1>\n") ;
        printf("</body>\n") ;
        printf("</html>\n") ;
        t=1;
} }
     if(t==0)
     {
printf("<html>\n") ;
        printf("<head><title>welcome</title></head>\n") ;
        printf("<body>\n") ;
        printf("<h1>sorry,you need the key!</h1>\n") ;
        printf("</body>\n") ;
        printf("</html>\n") ;}
    
sqlite3_finalize(stmt);
    
sqlite3_close(db); //关闭数据库
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;
}
   我在做的时候,遇到很多问题,程序编译使用arm-linux-gcc(4.3.2),程序一定可以编译通过,程序没有问题,主要是在编译的时候使用需要编译上sqlite3的一些库函数,和使用静态编译,如下命令;
#arm-linux-gcc -Wall -g -o 目标文件 源文件 -L.libs -static -lsqlite3
红题字一定放最后,网上说,不然会出现错误,我的没有出。

这样就可以把程序移植到BOA服务器的相关CGI脚本目录下,通过表单的action属性,设置触发路径。
需要在程序中修改你所要查询的数据库的路径。
如果在程序被触发后,网页显示各种错误提示,一般式BOA服务器配置出错了,可以看我相关BOA服务器配置文件的修改注意问题的文章。

相关推荐

ganyouxianjava / 0评论 2012-05-31