代码:
注意函数原型及其返回数值:
char* get_Cookie(const char* name)
{
char* cookie=0;
char* p=0;
int len=0;
p = getenv("HTTP_COOKIE") ;
if(!p)
{
return 0;
}
len = strlen(name);
cookie = (char*)malloc(len+2) ;
sprintf(cookie,"%s=",name);
len++;
if (p = strstr(p,cookie))
{
p=p+len ;
char* p2;
int len2=0;
if(p2=strstr(p,";"))len2=p2-p;
else len2=strlen(p);
if(len2>len) {
free(cookie);
cookie=(char*)malloc(len2+1);
}
memcpy(cookie,p,len2);
*(cookie+len2)=0;
}
else
{
free(cookie);
cookie=0;
}
return cookie;
}
返回的是char* 指针:
我们main函数:
#include <iostream>
int main(){
printf( "Set-Cookie: curentouturl=kkk...; path=/\n");
printf("Content-type:text/html\n\n");
//out_url.Format("%s", get_Cookie("curentouturl"));
char *tmp;
tmp =(char*)malloc(1024);
printf("%s",get_Cookie(tmp));
return 0;
}
注意函数原型及其返回数值:
char* get_Cookie(const char* name)
{
char* cookie=0;
char* p=0;
int len=0;
p = getenv("HTTP_COOKIE") ;
if(!p)
{
return 0;
}
len = strlen(name);
cookie = (char*)malloc(len+2) ;
sprintf(cookie,"%s=",name);
len++;
if (p = strstr(p,cookie))
{
p=p+len ;
char* p2;
int len2=0;
if(p2=strstr(p,";"))len2=p2-p;
else len2=strlen(p);
if(len2>len) {
free(cookie);
cookie=(char*)malloc(len2+1);
}
memcpy(cookie,p,len2);
*(cookie+len2)=0;
}
else
{
free(cookie);
cookie=0;
}
return cookie;
}
返回的是char* 指针:
我们main函数:
#include <iostream>
int main(){
printf( "Set-Cookie: curentouturl=kkk...; path=/\n");
printf("Content-type:text/html\n\n");
//out_url.Format("%s", get_Cookie("curentouturl"));
char *tmp;
tmp =(char*)malloc(1024);
printf("%s",get_Cookie(tmp));
return 0;
}
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/789/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表