歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

嵌入式ARM+SQLite3+BOA服務器用戶登錄驗證功能

近期做了嵌入式用戶登錄驗證功能的一個系統,所以把具體實現分享一下:   開發平台: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服務器配置文件的修改注意問題的文章。
Copyright © Linux教程網 All Rights Reserved