- 论坛徽章:
- 0
|
  
我在本机上安装了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
跪求 |
|