首页 | 软件中心 | Designand Inspiration
读库教程网 > 网络教程 > 网页编程 > CGI-Perl > CGI教程(7)解码数据发送给CGI脚本

CGI教程(7)解码数据发送给CGI脚本

添加:2007年5月10日
解码数据发送给CGI脚本

  当使用表单的时候,收集在表单的信息给发送给CGI脚本用于处理。这个信息被放置在环境变量QUERY_STRING中。

  为了清除地将信息传递给环境变量QUERY_STRING,被修改锚标签的表单将被使用。在这个被修改的锚标签中,传递给环境变量QUERY_STRING的数据是在指示CGI脚本的URL之后附上的。字符”?”被用来分隔指定CGI脚本以及发送给脚本的数据的URL。比如:

<A HREF="/cgi-bin/script?name=Your+name&action=find"> Link </A>

其中数据"name=Your+name&action=find"被放置在环境变量QUERY_STRING中并且CGI脚本被执行。

  下面给出一个例子:由C++编写一个类,具体文件parse.h 和parse.cpp被用于在QUERY_STRING中提取个别的组件,其中的头文件t99_type.h在上节教程已经提到,它是包含了一些定义。具体代码如下:

//以下是parse.h文件

#ifndef CLASS_PARSE

#define CLASS_PARSE

//#define NO_MAP // 定义没有用户处理

#include "t99_type.h"//这个文件在前面教程中有

class Parse

{

public:

Parse( char [] );

~Parse();

void set( char [] );

char *get_item( char [], int pos=1, bool=false );

char *get_item_n( char [], int pos=1, bool=false );

protected:

void remove_escape(char []);

int hex( char ); //返回十六进制数

char *map_uname( char [] );

private:

enum { SEP = '&' }; // 使用&分隔字符

char *the_str; // 字符部分

int the_length; // 字符长度

};

#endif

//以下是parse.cpp文件

#ifndef CLASS_PARSE_IMP

#define CLASS_PARSE_IMP

#include "parse.h"

#include

#include

#ifndef NO_MAP

# include

#endif

Parse::Parse( char list[] )

{

the_str = NULL;

set( list );

}

Parse::~Parse()

{

if ( the_str != NULL ) { // 释放存储器

delete [] the_str;

}

}

void Parse::set( char list[] )

{

if ( the_str != NULL ) { // 释放存储器

delete [] the_str;

}

the_length = strlen( list ); // 字符长度

the_str = new char[the_length+1]; // 分配空间

strcpy( the_str, list ); // 复制

}

char *Parse::get_item( char name[], int pos, bool file )

{

int len = strlen( name );

int cur_tag = 1;

for( int i=0; i
{

if ( the_str[i] == name[0] &&

strncmp( &the_str[i], name, len ) == 0 )

{

if ( the_str[i+len] == '=' )

{

if ( cur_tag == pos )

{

int start = i+len+1; int j = start;

while ( the_str[j] != SEP && the_str[j] != '\0' ) j++;

int str_len = j-start;

char *mes = new char[ str_len+1 ];

strncpy( mes, &the_str[start], str_len );

mes[str_len] = '\0';

remove_escape( mes );

# ifndef NO_MAP

return file ? map_uname(mes) : mes;

# else

return file ? mes : mes;

# endif

} else {

cur_tag++;
本内容共3页,当前第1页123

读库教程网文章由网络收集后整理发布,文章发布人拥有该内容的所有权力及责任!

如果你喜欢这页,可以按Ctrl+D收藏起来。

相关内容
相关评论
公益广告
精彩推荐
友情链接: 百分百青年 | 烛光信息网 | 夏布新网 | 新育互联网
管理员:QQ:27038219, E-mail:27038219@qq.com今日更新
读库教程网所有文章从网络收集所发布,文章发布人拥有该内容的所有权力及责任,转载时请注明出处!
Template designed by www.dkuu.com. Optimized for 1024x768 to Firefox,Opera and MS-IE6/IE7.