// Fig. 19.21: login.cpp // Program to output an XHTML form, verify the // username and password entered, and add members. #include using std::cerr; using std::cin; using std::cout; using std::ios; #include using std::fstream; #include using std::string; #include using std::getenv; using std::atoi; using std::exit; void header(); void writeCookie(); int main() { char query[ 1024 ] = ""; string dataString = ""; // strings to store username and password string userName = ""; string passWord = ""; int contentLength = 0; bool newMember = false; // data was posted if ( getenv( "CONTENT_LENGTH" ) ) { // retrieve query string contentLength = atoi( getenv( "CONTENT_LENGTH" ) ); cin.read( query, contentLength ); dataString = query; // find username location int userLocation = dataString.find( "user=" ) + 5; int endUser = dataString.find( "&" ); // find password location int passwordLocation = dataString.find( "password=" ) + 9; int endPassword = dataString.find( "&new" ); if ( endPassword > 0 ) // new membership requested { newMember = true; passWord = dataString.substr( passwordLocation, endPassword - passwordLocation ); } // end if else // existing member passWord = dataString.substr( passwordLocation ); userName = dataString.substr( userLocation, endUser - userLocation ); } // end if // no data was retrieved if ( dataString == "" ) { header(); cout << "

Please login.

"; // output login form cout << "
" << "

User Name:
" << "Password: " << "
New?

" << "
"; } // end if else // process entered data { string fileUsername = ""; string filePassword = ""; bool userFound = false; // open user data file for reading and writing fstream userData( "userdata.txt", ios::in | ios::out); if ( !userData ) // could not open file { cerr << "Could not open database."; exit( 1 ); } // end if // add new member if ( newMember ) { // read username and password from file while ( !userFound && userData >> fileUsername >> filePassword ) { if ( userName == fileUsername ) // name is already taken userFound = true; } // end while if ( userFound ) // user name is taken { header(); cout << "

This name has already been taken.

" << "Try Again"; } // end if else // process data { writeCookie(); // write cookie header(); // write user data to file userData.clear(); // clear eof, allow write at end of file userData << "\n" << userName << "\n" << passWord; cout << "

Your information has been processed." << "Start Shopping

"; } // end else } // end if else // search for password if entered { bool authenticated = false; // read in user data while ( !userFound && userData >> fileUsername >> filePassword ) { // username was found if ( userName == fileUsername ) { userFound = true; // determine whether password is correct // and assign bool result to authenticated authenticated = ( passWord == filePassword ); } // end if } // end while // user is authenticated if ( authenticated ) { writeCookie(); header(); cout << "

Thank you for returning, " << userName << "!

" << "Start Shopping"; } // end if else // user not authenticated { header(); if ( userFound ) // password is incorrect cout << "

You have entered an incorrect password. " << "Please try again.

" << "Back to login"; else // user is not registered cout << "

You are not a registered user.

" << "Register"; } // end else } // end else } // end else cout << "\n\n"; return 0; } // end main // function to output header void header() { cout << "Content-Type: text/html\n\n"; // output header // output XML declaration and DOCTYPE cout << "" << ""; // output html element and some of its contents cout << "" << "Login Page"; } // end function header // function to write cookie data void writeCookie() { string expires = "Friday, 14-MAY-10 16:00:00 GMT"; cout << "Set-Cookie: CART=; expires=" << expires << "; path=\n"; } // end function writeCookie /************************************************************************** * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/