免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2214 | 回复: 3
打印 上一主题 下一主题

[Web] CGI脚本不能正常读取和写入cookie [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-12 16:45 |只看该作者 |倒序浏览

我在本机上安装了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

跪求

论坛徽章:
0
2 [报告]
发表于 2006-10-12 16:46 |只看该作者
Sorry,不知怎么打错了,应为getenv("HTTP_COOKIE");

论坛徽章:
0
3 [报告]
发表于 2006-10-12 17:29 |只看该作者
>>跪求
严重了, 有问题大家都会热心帮你地.


  1.    // expiration date of cookie
  2.    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 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-10-13 18:29 |只看该作者
thanks
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP