cgic问题,麻烦看看

cgic问题,麻烦看看

麻烦看看如下程序:

test1:
#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#if 1
#define SERVER_NAME cgiServerName
#endif
void CookieSet();
int cgiMain()
{
         
        CookieSet();
        cgiHeaderContentType("text/html");

         printf("<form name=\"form1\" method=\"post\" action=\"test2\">");
         printf("<input name=cname type=text  size=15 maxlength=8>");
         printf("<br>");
         printf("<input name=cvalue type=text  size=15 maxlength=8>");
         printf("<input name=\"Submit\" type=\"submit\" class=\"button\" value=\"提交\">");
         printf("</form>");
       
        return 0;
}

void CookieSet()
{
        char cname[1024];
        char cvalue[1024];
        /* Must set cookies BEFORE calling cgiHeaderContentType */
        cgiFormString("cname", cname, sizeof(cname));       
        cgiFormString("cvalue", cvalue, sizeof(cvalue));
        //printf("strlen(cname)=%d\n",strlen(cname));       
        if (strlen(cname)) {
                /* Cookie lives for one day (or until browser chooses
                        to get rid of it, which may be immediately),
                        and applies only to this script on this site. */       
                cgiHeaderCookieSetString(cname, cvalue,
                        86400, cgiScriptName, SERVER_NAME);
        }
}


test2:

#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>

void Cookies();
int cgiMain()
{
       
        cgiHeaderContentType("text/html");
       
        Cookies();
        printf("<a href=test3>111</a>");
        return 0;
}

void Cookies()
{
        char **array, **arrayStep;
        char cname[1024], cvalue[1024];
       
        if (cgiCookies(&array) != cgiFormSuccess) {
                return;
        }
        printf("*arrayStep=%d\n",*arrayStep);
        arrayStep = array;
        fprintf(cgiOut, "<table border=1>\n");
        fprintf(cgiOut, "<tr><th>Cookie<th>Value</tr>\n");
        while (*arrayStep) {
                char value[1024];
                fprintf(cgiOut, "<tr>");
                fprintf(cgiOut, "<td>");
                cgiHtmlEscape(*arrayStep);
                fprintf(cgiOut, "<td>");
                cgiCookieString(*arrayStep, value, sizeof(value));
                cgiHtmlEscape(value);
                fprintf(cgiOut, "\n");
                arrayStep++;
        }
        fprintf(cgiOut, "</table>\n");
        cgiFormString("cname", cname, sizeof(cname));       
        cgiFormString("cvalue", cvalue, sizeof(cvalue));       
        if (strlen(cname)) {
                fprintf(cgiOut, "New Cookie Set On This Call:<p>\n");
                fprintf(cgiOut, "Name: ");       
                cgiHtmlEscape(cname);
                fprintf(cgiOut, "Value: ");       
                cgiHtmlEscape(cvalue);
               
               
        }
        cgiStringArrayFree(array);
}
我不明白为什么printf("*arrayStep=%d\n",*arrayStep);为0
cookie没有保存我的内容?请问用过cgic库的大侠指点下


test3:
#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>

void Cookies();
int cgiMain()
{
       
        cgiHeaderContentType("text/html");
       
        Cookies();
        return 0;
}

void Cookies()
{
        char **array, **arrayStep;
        char cname[1024], cvalue[1024];
        if (cgiCookies(&array) != cgiFormSuccess) {
                return;
        }
        arrayStep = array;
        fprintf(cgiOut, "<table border=1>\n");
        fprintf(cgiOut, "<tr><th>Cookie<th>Value</tr>\n");
        printf("*arrayStep=%d\n",*arrayStep);
        while (*arrayStep) {
                char value[1024];
                fprintf(cgiOut, "<tr>");
                fprintf(cgiOut, "<td>");
                cgiHtmlEscape(*arrayStep);
                fprintf(cgiOut, "<td>");
                cgiCookieString(*arrayStep, value, sizeof(value));
                printf("value=%s\n",value);
                cgiHtmlEscape(value);
                fprintf(cgiOut, "\n");
                arrayStep++;
        }
        fprintf(cgiOut, "</table>\n");
        cgiFormString("cname", cname, sizeof(cname));       
        cgiFormString("cvalue", cvalue, sizeof(cvalue));       
        if (strlen(cname)) {
                fprintf(cgiOut, "New Cookie Set On This Call:<p>\n");
                fprintf(cgiOut, "Name: ");       
                cgiHtmlEscape(cname);
                fprintf(cgiOut, "Value: ");       
                cgiHtmlEscape(cvalue);
                fprintf(cgiOut, "<p>\n");
                fprintf(cgiOut, "If your browser accepts cookies (many do not), this new cookie should appear in the above list the next time the form is submitted.<p>\n");
        }
        //cgiStringArrayFree(array);
}
使用 cookie 需要注意几点:
1. 过期时间, 不要设置成过去, 这种操作是删除 cookie
2. 作用对象, 是必须是同一对象. 如
  http://ip/test1 不能为 http://ip/test2 设置 cookie.

明白你错在哪里了吧.
另外, 再给你一个简单的 cookie 的示例:
这个例子上有两个输入框, 两个按钮,  分别用于设置, 我查询 cookie.

#!/usr/bin/cspengine
<html>
<body>
<% /* user click set command */
if (!isblankstr(G("set"))) {
        setcookie(G("ckname"), G("ckvalue"), NULL);
} %>

<% /* user click query command */
if (!isblankstr(G("query"))) { %>
    name: <% =G("ckname") %><br>
    value: <% =getcookie(G("ckname")) %><br>
<% } %>

<form method=post action=<% =thisCgiPrefix() %>>
Cookie name: <input type=input name=ckname><p>
Cookie value: <input type=input name=ckvalue><p>
<input type=submit value="Set" name=set>
<input type=submit value="Query" name=query>
</form>
</body>
</html>
没用过cgic库,习惯自己写,帮你顶
谢谢newzy的指点,我还是不明白:
2. 作用对象, 是必须是同一对象. 如
http://ip/test1 不能为 http://ip/test2 设置 cookie.

是什么意思?我在test1设置cookie在test2,test3读出cookie不对吗?