[問題] 用C++編寫的cgi

看板C_and_CPP作者 (一期一会)時間15年前 (2010/04/13 20:21), 編輯推噓1(1018)
留言19則, 6人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚)設計一個帳號密碼的登入系統, 能記帳號,並且在帳密輸入以後 導向表格輸入的頁面,不過現在光是 由G++ compile好的.cgi本身, 就無法瀏覽其內容,在此求教於各位大大。 (附上.cpp的原始碼, 大致是參考Deitel的書。) 希望得到的正確結果:開啟cgi頁面 程式跑出來的錯誤結果: 用firefox執行的結果是會叫我下載.cgi檔OB 開發平台: g++ on Ubuntu 有問題的code: (請善用置底文標色功能) // Program to output an XHTML form, verify the // username and password entered, and add members. #include <iostream> using std::cerr; using std::cin; using std::cout; using std::ios; #include <fstream> using std::fstream; #include <string> using std::string; #include <cstdlib> 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 << "<p>Please login.</p>"; // output login form cout << "<form method = \"post\" action = \"/cgi-bin/login.cgi\">" << "<p>User Name: <input type = \"text\" name = \"user\"/><br/>" << "Password: <input type = \"password\" name = \"password\"/>" << "<br/>New? <input type = \"checkbox\" name = \"new\"" << " value = \"1\"/></p>" << "<input type = \"submit\" value = \"login\"/></form>"; } // 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 << "<p>This name has already been taken.</p>" << "<a href=\"/cgi-bin/login.cgi\">Try Again</a>"; } // 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 << "<p>Your information has been processed." << "<a href=\"/cgi-bin/shop.cgi\">Start Shopping</a></p>"; } // 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 << "<p>Thank you for returning, " << userName << "!</p>" << "<a href=\"/cgi-bin/form.html\">Start to enter text</a>"; } // end if else // user not authenticated { header(); if ( userFound ) // password is incorrect cout << "<p>You have entered an incorrect password. " << "Please try again.</p>" << "<a href=\"/cgi-bin/login.cgi\">Back to login</a>"; else // user is not registered cout << "<p>You are not a registered user.</p>" << "<a href=\"/cgi-bin/login.cgi\">Register</a>"; } // end else } // end else } // end else cout << "</body>\n</html>\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 << "<?xml version = \"1.0\"?>" << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" " << "\"";" rel="nofollow">http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"; // output html element and some of its contents cout << "<html xmlns = \""" rel="nofollow">http://www.w3.org/1999/xhtml\">" << "<head><title>Login Page</title></head><body>"; } // 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 補充說明: -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.174.136

04/13 20:47, , 1F
你的httpd沒有設.cgi的handler?
04/13 20:47, 1F

04/13 20:49, , 2F
還沒上傳 只是放在本機裡面試跑
04/13 20:49, 2F

04/13 21:02, , 3F
一樣要設 (在本機裡設)
04/13 21:02, 3F

04/13 21:57, , 4F
感謝一三樓兩位大大 請問要怎麼設定呢?
04/13 21:57, 4F

04/13 23:10, , 5F
這可以參考看看:http://goo.gl/FZGg
04/13 23:10, 5F

04/13 23:29, , 6F
感恩
04/13 23:29, 6F

04/13 23:32, , 7F
按照五樓reference裡的指令在終端機執行,卻找不到指令
04/13 23:32, 7F

04/13 23:33, , 8F
不是執行喔,他是要你修改 Apache 的設定檔 httpd.conf
04/13 23:33, 8F

04/13 23:34, , 9F
(不過我記得新版 Apache 是不是換設定檔位置了,要找一下)
04/13 23:34, 9F

04/13 23:34, , 10F
你的本機有跑 Apache 起來了嗎?
04/13 23:34, 10F

04/13 23:34, , 11F
Ubuntu裡面有Apache這種東西啊?
04/13 23:34, 11F

04/13 23:35, , 12F
抱歉,我這是第一次學架伺服器 有很多地方不懂
04/13 23:35, 12F

04/14 00:07, , 13F
Apache 是 Linux 上的網頁伺服器,你的 CGI 必須透過他才
04/14 00:07, 13F

04/14 00:07, , 14F
能在網站上給別人看,就算在本機測試也是要架,否則就是
04/14 00:07, 14F

04/14 00:08, , 15F
會變成跟你目前一樣只有下載檔案 (Apache 架好後還要設定)
04/14 00:08, 15F

04/14 00:08, , 16F
建議可以先到 Linux 板逛看看,熟悉一下怎麼裝 Apache :)
04/14 00:08, 16F

04/14 00:09, , 17F
google搜尋「鳥哥」也是很好的入門資源
04/14 00:09, 17F

04/14 08:56, , 18F
這是純軟體設定的問題。
04/14 08:56, 18F

04/15 16:09, , 19F
感謝各位
04/15 16:09, 19F
文章代碼(AID): #1Bn67AwG (C_and_CPP)