| 域名空间 下载中心 社区论坛 信息公告 MY小屋 |
![]() |
联系我们 设为首页 加入收藏 |
|
首页 | 新闻资讯 | 编程开发 | 网页设计 | 图形图象 | 网络媒体 | 网站模板 | 数 据 库 | 投稿 论坛 | 操作系统 | 系统优化 | 网络安全 | 黑客技术 | 硬件学堂 | 硬件报价 | 服 务 器 | 地图 专题 | 应用软件 | 聊天通讯 | Q Q 专栏 | 建站经验 | 在线工具 | 站长Club | 注 册 表 | 旧版 社会 | 游戏娱乐 | 设计欣赏 | 疑难解答 | 社区论坛 | 韩国素材 | 素材图库 | 广告服务 | 服务 |
| 新版上线![旧版] | |||||
注:打开慢时请稍等
CGI教程(7)解码数据发送给CGI脚本之一http://www.iyit.net 日期:2006-6-12 2:06:09 来源: 点击: |
当使用表单的时候,收集在表单的信息给发送给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++; } } } } return NULL; } char *Parse::get_item_n( char name[], int pos, bool file ) { char *res = get_item( name, pos, file ); return res == NULL ? (char*)"----" : res; } void Parse::remove_escape(char str[]) { char * from = &str[0]; char * to = &str[0]; while ( *from != '\0' ) { char ch = *from++; switch ( ch ) { case '%' : ch = (hex(*from++)><4) | hex(*from++); break; case '+' : ch = ' '; break; } *to++ = ch; } *to = '\0'; } int Parse::hex( char c ) { if ( isdigit( c ) ) return c-'0'; if ( isalpha( c ) ) return tolower(c)-'a'+10; return 0; } char* Parse::map_uname( char *path ) { #ifndef NO_MAP if ( path[0] == '~' ) { char uname[255]; // 容纳用户名字的变量 char *rest = &path; char *p = &uname[0]; while ( *rest != '/' && *rest != '\0' ) { *p++ = *rest++; } *p = '\0'; // 结束标识 char *root = uname; passwd *pw = getpwnam( uname ); if ( pw != NULL ) { root = pw->pw_dir; // 用户的主目录 } int len_root = strlen(root); int len_path = len_root + strlen(rest); char *new_path = new char[len_path+1]; // 动态字符 strcpy( &new_path[0], root ); // 复制用户路径 strcpy( &new_path[len_root], rest ); // 其余部分 return new_path; } else { return path; } #endif return path; } #endif 编辑:黑鹰 [发送给好友] [打印本页] [关闭窗口] [返回顶部] 上一篇:CGI教程(7)解码数据发送给CGI脚本之二 下一篇:CGI教程(6)调用CGI脚本文件的例子之四 转载请注明来源:www.iyit.net 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。 |
| 最新更新 | 热点排行 | 推荐新闻 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
| 友情链接 | ||||||
| 设置首 页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接 |
| |||||||