// Fig. 16.17: readcookie.cpp // Program to read cookie data. #include using std::cout; using std::cin; #include #include 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 << "" << ""; // output html element and some of its contents cout << "" << "Read Cookies" << ""; // data was found if ( dataString != "" ) cout << "

The following data is saved in a cookie on" << " your computer

" << "

Name: " << nameString << "

" << "

Age: " << ageString << "

" << "

Color: " << colorString << "

"; // no data was found else cout << "

No cookie data.

"; cout << ""; return 0; } // end main