嵌入式服务器,数据库下登陆功能实现

一 平台介绍

硬件:s3c2410开发板(我用的是友善之臂的)、pc机。

软件:boa服务器、sqlite数据库(需要把服务器和数据库都配置好)

二程序源代码

源代码如下:

嵌入式服务器,数据库下登陆功能实现_第1张图片

(1)index.html

<html><meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<head><title>用户登陆验证</title></head>
<body>
<form name="form1" action="/login/index.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>

(2)index.c(该文件可以生成index.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=
             // 在"Username="和"&"之间的是我们要取出来的用户名
             for ( i = 9; i < (int)strlen(input); i++ )
             {
                            if ( input[i] == '&' )
                            {
                                         namein[j] = '\0';
                                         break; //跳出for循环,因为用户名已经取得。
                            }                                   
                            namein[j++] = input[i];
             }
         // 前面9个字符 + "&Password="10个字符
             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("login.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件

//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
char *sql = " CREATE TABLE user(ID INTEGER PRIMARY KEY,Name text,Password text);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
   
//查询数据
int ret=0;
int t=0;
sql = "SELECT Password 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;
}

(3)Makefile文件

# Makefile created by MaShang(gcc version 4.4.3)

CPP  = arm-linux-g++
CC   = arm-linux-gcc
WINDRES =
RES  =
OBJ  = index.o  $(RES)
LINKOBJ  = index.o $(RES)
LIBS += -L ./lib -lsqlite3 -lpthread -ldl
INCS += -I ./include
CXXINCS =
BIN  = index.cgi
CXXFLAGS = $(CXXINCS) 
CFLAGS = $(INCS) 
RM = rm -f

.PHONY: all clean clean-custom

all: index.cgi


clean: clean-custom
 ${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
 $(CC) -static  -g -o index.cgi index.c $(LIBS) $(CFLAGS)

index.o: index.c
 $(CC) -g -c index.c -o index.o $(CFLAGS)

 

(4)程序源代码的主题已经完成了,lib文件夹里面是库文件,include文件夹里是.h 文件

(5)数据库login.db

BEGIN TRANSACTION;
CREATE TABLE user(Id integer PRIMARY KEY,Name text,Password text);
INSERT INTO user VALUES(1,'root',123456);
INSERT INTO user VALUES(2,'mashang',123456789);
COMMIT;

 

最后在linux系统Fedora9下面实现了整个过程。

你可能感兴趣的:(嵌入式服务器,数据库下登陆功能实现)