歡迎您光臨本站 註冊首頁

cgic問題,麻煩看看

←手機掃碼閱讀     火星人 @ 2014-03-05 , reply:0

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;
        char cvalue;
        /* 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, cvalue;
       
        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;
                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, cvalue;
        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;
                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不對嗎?

[ 本帖最後由 bjiang 於 2007-3-19 13:21 編輯 ]

[火星人 ] cgic問題,麻煩看看已經有603次圍觀

http://coctec.com/docs/service/show-post-40500.html