ARM11 boa服务器用户登录验证

本文改自:http://3633188.blog.51cto.com/3623188/826594

ARM板:Tiny6410

开发环境:UBUNTU10.10

数据库版本:Sqlite3.3.7.13

BOA版本:Tiny6410自带

1.网页设计,网页设计是直接从网上下载的一个登录界面,再稍加修改而成。效果如下所示:

ARM11 boa服务器用户登录验证_第1张图片

下载链接:http://www.mobanwang.com/mb/201008/8390.html

2. CGI代码实现

新建login.c文件,如下:

#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 sqlStatement[512];
	char *zErrMsg;
	int nrow = 0, ncolumn = 0;
	char **azResult;//二维数组存放结果
	int rc;

	char userName[20]; 
	char userPasswd[20]; 
	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"&userPasswd="aaaaa" 
	// 其中"Username="和"&Password="都是固定的 
	// 而"admin"和"aaaaa"都是变化的,也是我们要获取的 

	// 前面9个字符是userName= 
	// 在"userName="和"&"之间的是我们要取出来的用户名 
	for ( i = 9; i < (int)strlen(input); i++ ) 
	{ 
		if ( input[i] == '&' ) 
		{ 
			userName[j] = '\0'; 
			break; 
		}                                    
		userName[j++] = input[i]; 
	} 
	// 前面9个字符 + "&userPasswd="12个字符 + userName的字符数 
	// 是我们不要的,故省略掉,不拷贝 
	for ( i = 21 + strlen(userName), j = 0; i < (int)strlen(input); i++ ) 
	{ 
		if(input[i] == '&')
		{
			userPasswd[j] = '\0';
			break;
		}
		userPasswd[j++] = input[i]; 
	} 
    
	sqlite3 *db=NULL; 
	rc = sqlite3_open("info.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件 
	if(rc)
	{
		fprintf(stderr,"Can't open database:%s\n",sqlite3_errmsg(db));
		sqlite3_close(db);
	}
	strcpy(sqlStatement, "SELECT userPasswd FROM user_table WHERE userName='"); 
	strcat(sqlStatement, userName);
	strcat(sqlStatement, "'");

	rc = sqlite3_get_table(db, sqlStatement, &azResult, &nrow, &ncolumn, &zErrMsg);
	if(rc != SQLITE_OK)
	{
		fprintf(stderr, "SQL error:%s\n",zErrMsg);
	}

	if(nrow == 1)
	{
		if(strcmp(userPasswd, azResult[1]) == 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") ; 
		}
		else
		{
			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") ;
		}
	}
	else if(nrow == 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_free_table(azResult);
	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.交叉编译该代码:arm-linux-gcc -o login.cgi login.c -I /key_C/sqlite/include/ -L /key_C/sqlite/lib/ -lsqlite3,将login.cgi文件以及网页文件放到/www目录下。

4. 在/www目录下新建info.db数据库文件,并插入一条数据,即可进行用户登录验证。

你可能感兴趣的:(ARM11 boa服务器用户登录验证)