PDA

View Full Version : کار با فایل در CGI



ICEMAN
پنج شنبه 02 اسفند 1386, 15:57 عصر
سلام ...
من یه کم CGI کار کردم ولی یه از تمرین های این مقاله برای ساختن فایل روی سرور هست که اطلاعات ورودی رو Save میکنه .txt

فایل HTML :


<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<!-- Fig. 19.18: savefile.html -->
<!-- Form to input client information -->

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Please enter your contact information</title>
</head>

<body>
<p><u><i>Please enter your information in the form below.</i></u></p>
<p><u>Note: You must fill in all fields.</u></p>
<form method = "post" action = "/cgi-bin/savefile.exe">
<p>
First Name:
<input type = "text" name = "firstname" size = "10" />
Last Name:
<input type = "text" name = "lastname" size = "15" />
</p>
<p>
Address: <input type = "text" name = "address" size = "25" /><br />
Town: <input type = "text" name = "town" size = "10" />
State: <input type = "text" name = "state" size = "2" /><br/>
Zip Code: <input type = "text" name = "zipcode" size = "5" />
Country: <input type = "text" name = "country" size = "10" />
</p>
<p>
E-mail Address: <input type = "text" name = "email" />
</p>
<input type = "submit" value = "Enter" />
<input type = "reset" value = "Clear" />
</form>
</body>
</html>


کد c++ (CGI)


#include <iostream>
using std:: cerr;
using std:: cin;
using std:: cout;
using std:: ios;
#include <fstream>
using std:: ofstream;
#include <string>
using std:: string;
#include <cstdlib>
using std:: getenv;
using std:: atoi;
using std:: exit;

int main()
{
char postString[ 1024 ]= "";
int contentLength= 0;

string dataString= "";
string firstname= "";
string lastname= "";
string address= "";
string town= "";
string state= "";
string zipcode= "";
string country= "";
string email= "";

if ( getenv( "CONTENT_LENGTH" ) )
contentLength= atoi( getenv( "CONTENT_LENGTH" ) );

cin.read( postString, contentLength );
dataString= postString;

string:: size_type charLocation= dataString.find( "+" );

while ( charLocation < string:: npos )
{
dataString.replace( charLocation, 1, " " );
charLocation= dataString.find( "+", charLocation + 1 );
}
/////////////first
int firstStart= dataString.find( "firstname=" ) + 10;
int endFirst= dataString.find( "&lastname" );
firstname= dataString.substr(
firstStart, endFirst - firstStart );
/////////////last
int lastStart= dataString.find( "lastname=" ) + 9;
int endLast= dataString.find( "&address" );
lastname= dataString.substr(
lastStart, endLast - lastStart );
/////////////address
int addressStart= dataString.find( "address=" ) + 8;
int endAddress= dataString.find( "&town" );
address= dataString.substr(
addressStart, endAddress - addressStart );
/////////////town
int townStart= dataString.find( "town=" ) + 5;
int endTown= dataString.find( "&state" );
town= dataString.substr(
townStart, endTown - townStart );
/////////////state
int stateStart= dataString.find( "state=" ) + 6;
int endState= dataString.find( "&zipcode" );
state= dataString.substr(
stateStart, endState - stateStart );
/////////////zipcode
int zipStart= dataString.find( "zipcode=" ) + 8;
int endZip= dataString.find( "&country" );
zipcode= dataString.substr(
zipStart, endZip - zipStart );
/////////////country
int countryStart = dataString.find( "country=" ) + 8;
int endCountry = dataString.find( "&email" );
country = dataString.substr(
countryStart, endCountry - countryStart );
/////////////email
int emailStart = dataString.find( "email=" ) + 6;
int endEmail = dataString.find( "&submit" );
email = dataString.substr(
emailStart, endEmail - emailStart );

cout <<"NOOOOOOOOOO";
cout << "Content-Type: text/html\n\n";

cout << "<?xml version = \"1.0\"?>"
<< "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
<< "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";

///////////////writting file
ofstream outFile( "clients.txt", ios:: app );
if (!outFile )
{
cerr <<"Error: could not open contact file.";
exit( 1 );
}
outFile <<firstname <<" " <<lastname <<"\n"
<<town <<" " <<state <<" " <<country <<" " <<zipcode <<"\n"
<<email <<"\n\n";

cout << "<table><tbody><tr><td>First Name:</td><td>" << firstname
<< "</td></tr><tr><td>Last Name:</td><td>" << lastname
<< "</td></tr><tr><td>Address:</td><td>" << address
<< "</td></tr><tr><td>Town:</td><td>" << town
<< "</td></tr><tr><td>State:</td><td>" << state
<< "</td></tr><tr><td>Zip Code:</td><td>" << zipcode
<< "</td></tr><tr><td>Country:</td><td>" << country
<< "</td></tr><tr><td>Email:</td><td>" << email
<< "</td></tr></tbody>"
<<"</table></body>\n</html>\n";

return 0;
}


این اون مثال هست روی IIS و Apache 2.2.8 هم تست شده با IE و FireFox
خطا در IE


The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Parameter entity must be defined before it is used. Error processing resource 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd...

%xhtml-prefw-redecl.mod;
-^


توی FireFox هم که فایل .cgi یا exe رو میده برای download

ولی حتی یه برنامه کوچک برای ساختن فایل هم اجرا نشد