歡迎您光臨本站 註冊首頁

CGI腳本不能正常讀取和寫入cookie

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

CGI腳本不能正常讀取和寫入cookie

:cry::cry::cry:
我在本機上安裝了apahce2.2,測試用C++編寫的CGI腳本.結果運行了writecookie.cgi后在
C:\Documents and Settings\Administrator\Cookies下找不到寫入的cookie,運行readcookie.cgi后又無法讀取環境變數(運行到getenv("HTTP_COOKIE")時出錯),可能是什麼原因啊,暈死.
如果懂C++的,不妨看一看腳本的源代碼:

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!-- Fig. 16.15: cookieform.html -->
<!-- Cookie Demonstration         -->
   
<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>Writing a cookie to the client computer</title>
   </head>
   
   <body>
      <h1>Click Write Cookie to save your cookie data.</h1>
      
      <form method = "post"
         action = "/cgi-bin/writecookie.cgi">
      
         <p>Name:<br />
            <input type = "text" name = "name" />
         </p>
      
         <p>Age:<br />
            <input type = "text" name = "age" />
         </p>
      
         <p>Favorite Color:<br />
            <input type = "text" name = "color" />
         </p>
      
         <p>
            <input type = "submit" name = "button" />
         </p>
      </form>
      
   </body>
</html>


//  writecookie.cpp
// Program to write a cookie to a client's machine.
#include <iostream>

using std::cout;
using std::cin;

#include <cstdlib>
#include <string>

using std::string;

int main()
{
   char query[ 1024 ] = "";
   string dataString = "";
   string nameString = "";
   string ageString = "";
   string colorString = "";
   
   int contentLength = 0;
   
   // expiration date of cookie
   string expires = "Friday, 14-MAY-04 16:00:00 GMT";
   
   // data was entered
   if ( getenv( "CONTENT_LENGTH" ) ) {
      contentLength = atoi( getenv( "CONTENT_LENGTH" ) );

      // read data from standard input
      cin.read( query, contentLength );
      dataString = query;

      // search string for data and store locations
      int nameLocation = dataString.find( "name=" ) + 5;
      int endName = dataString.find( "&" );
      
      int ageLocation = dataString.find( "age=" ) + 4;
      int endAge = dataString.find( "&color" );
      
      int colorLocation = dataString.find( "color=" ) + 6;
      int endColor = dataString.find( "&button" );
      
      // get value for user's name
      nameString = dataString.substr( nameLocation, endName -
         nameLocation );
      
      // get value for user's age      
      if ( ageLocation > 0 )
         ageString = dataString.substr( ageLocation, endAge -
            ageLocation );
      
      // get value for user's favorite color
      if ( colorLocation > 0 )
         colorString = dataString.substr( colorLocation,
            endColor - colorLocation );

      // set cookie
      cout << "Set-Cookie: Name=" << nameString << "age:"
           << ageString << "color:" << colorString
           << "; expires=" << expires << "; path=\n";
   
   } // end if
      
   // output header
   cout << "Content-Type: text/html\n\n";
   
   // output XML declaration and DOCTYPE
   cout << "<?xml version = \"1.0\"?>"
        << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 "
        << "Transitional//EN\" \"http://www.w3.org/TR/xhtml1"
        << "/DTD/xhtml1-transitional.dtd\">";

   // output html element and some of its contents
   cout << "<html xmlns = \"http://www.w3.org/1999/xhtml\">"
        << "<head><title>Cookie Saved</title></head>"
        << "<body>";
   
   // output user's information
   cout << "<p>The cookies have been set with the following"
        << " data:</p>"
        << "<p>Name: " << nameString  << "<br/></p>"
        << "<p>Age:"   << ageString   << "<br/></p>"
        << "<p>Color:" << colorString << "<br/></p>"
        << "<p>Click <a href=\"/cgi-bin/readcookie.cgi\">"
        << "here</a> to read saved cookie data:</p>"
        << "</body></html>";
      
   return 0;

} // end main


// Fig. 16.17: readcookie.cpp
// Program to read cookie data.
#include <iostream>

using std::cout;
using std::cin;

#include <cstdlib>
#include <string>

using std::string;

int main()
{
   string dataString = "";
   string nameString = "";
   string ageString = "";
   string colorString = "";
   
   dataString= getenv( "HTTP_COOKIE" ); // 不能正常運行!!!
      
   // search through cookie data string
   int nameLocation = dataString.find( "Name=" ) + 5;
   int endName = dataString.find( "age:" );
   
   int ageLocation = dataString.find( "age:" ) + 4;
   int endAge = dataString.find( "color:" );
   
   int colorLocation = dataString.find( "color:" ) + 6;
   
   // store cookie data in strings
   nameString = dataString.substr( nameLocation, endName -
      nameLocation );
   ageString = dataString.substr( ageLocation, endAge -
      ageLocation);
   colorString = dataString.substr( colorLocation );
   
   // output header
   cout << "Content-Type: text/html\n\n";
   
   // output XML declaration and DOCTYPE
   cout << "<?xml version = \"1.0\"?>"
        << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 "
        << "Transitional//EN\" \"http://www.w3.org/TR/xhtml1"
        << "/DTD/xhtml1-transitional.dtd\">";

   // output html element and some of its contents
   cout << "<html xmlns = \"http://www.w3.org/1999/xhtml\">"
        << "<head><title>Read Cookies</title></head>"
        << "<body>";
   
   // data was found
   if ( dataString != "" )
      cout << "<h3>The following data is saved in a cookie on"
           << " your computer</h3>"
           << "<p>Name: "  << nameString  << "<br/></p>"
           << "<p>Age: "   << ageString   << "<br/></p>"
           << "<p>Color: " << colorString << "<br/></p>";

   // no data was found
   else
      cout << "<p>No cookie data.</p>";
   
   cout << "</body></html>";
   
   return 0;

} // end main

跪求
《解決方案》

Sorry,不知怎麼打錯了,應為getenv("HTTP_COOKIE");
《解決方案》

>>跪求
嚴重了, 有問題大家都會熱心幫你地.


   // expiration date of cookie
   string expires = "Friday, 14-MAY-04 16:00:00 GMT";


錯誤原因, 是你把過期時間設置成了過去時, 系統根本沒有存儲.
一般的, 設置成現在加上一個值, 如一天後過期:
time(NULL) + 3600*24
將它轉換成 GMT 格式.

C 寫 CGI 試試 eybuild 吧, 直接調用 setcookie() 和 getcookie() 即可.
這裡包含 cookie 的實例: http://bbs.chinaunix.net/viewthread.php?tid=834592&extra=page%3D1

祝你好運!

[ 本帖最後由 newzy 於 2006-10-12 17:32 編輯 ]
《解決方案》

thanks

[火星人 ] CGI腳本不能正常讀取和寫入cookie已經有510次圍觀

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