Page 1 of 1

cgi post example requested

Posted: Thu Jul 08, 2004 7:06 am
by griz
Hi guys,

I would like to write a CGI utility that can pull POST values from an html form. I have done this with GET and have searched through the forums for some simple examples without luck. Can any of you who are experienced with PB-CGI-POST provide a simple example? How can I retrieve the "userid" and "password" values below?

Code: Select all

<html><body><center>

<FORM METHOD="POST" ACTION="checker.exe" ENCTYPE="application/x-www-form-urlencoded">
User ID : 
<INPUT TYPE="text" NAME="userid" VALUE="" SIZE="20" MAXLENGTH="100"> 
<p>
Password : 
<INPUT TYPE="password" NAME="password" VALUE="" SIZE="20" MAXLENGTH="100"> 
<p>
<INPUT TYPE="submit" NAME="submit" VALUE="Submit">
</form>

</center></body></html>

Posted: Thu Jul 08, 2004 11:57 am
by El_Choni
Simple example, tested with Apache 2.0.50 (compile in console mode to your cgi-bin dir):

Code: Select all

hInput = GetStdHandle_(#STD_INPUT_HANDLE)
SetConsoleMode_(hInput, #ENABLE_LINE_INPUT|#ENABLE_ECHO_INPUT|#ENABLE_PROCESSED_INPUT)
BufferStep = 1024
BufferSize = BufferStep
*Buffer = AllocateMemory(BufferSize)
*BufferOffset = *Buffer
While ReadFile_(hInput, *BufferOffset, BufferStep-1, @bRead, 0)
  BufferSize+bRead
  *Buffer = ReAllocateMemory(*Buffer, BufferSize)
  *BufferOffset = *Buffer+BufferSize-BufferStep
Wend
Input$ = PeekS(*Buffer)
FreeMemory(*Buffer)
CloseHandle_(hInput)
hOutput = GetStdHandle_(#STD_OUTPUT_HANDLE)
HttpAnswer$ = "Content-type: text/html"+Chr(10)+Chr(13)+Chr(10)+Chr(13)+"<html><header><title>Answer from CGI</title></header><body>Hi, received your data:<br>"
HttpAnswer$+StringField(Input$, 1, "&")+"<br>"+StringField(Input$, 2, "&")
HttpAnswer$+"</body></html>"
WriteFile_(hOutput, @HttpAnswer$, Len(HttpAnswer$), @Written, 0)
CloseHandle_(hOutput)

Posted: Thu Jul 08, 2004 4:50 pm
by Paul
Nice code El_Choni :)

Just a quick note, StringField index starts at 1 so you need to adjust this line to...

Code: Select all

HttpAnswer$+StringField(Input$, 1, "&")+"<br>"+StringField(Input$, 2, "&")

Posted: Thu Jul 08, 2004 6:08 pm
by El_Choni
Better now, thx Paul:

Code: Select all

hInput = GetStdHandle_(#STD_INPUT_HANDLE)
SetConsoleMode_(hInput, #ENABLE_LINE_INPUT|#ENABLE_ECHO_INPUT|#ENABLE_PROCESSED_INPUT)
*ContentLength = AllocateMemory(128)
ContentLength = Val(PeekS(*ContentLength, GetEnvironmentVariable_("CONTENT_LENGTH", *ContentLength, 128)))
FreeMemory(*ContentLength)
*Buffer = AllocateMemory(ContentLength)
ReadFile_(hInput, *Buffer, ContentLength, @bRead, 0)
Input$ = PeekS(*Buffer)
FreeMemory(*Buffer)
CloseHandle_(hInput)
hOutput = GetStdHandle_(#STD_OUTPUT_HANDLE)
HttpAnswer$ = "Content-type: text/html"+Chr(10)+Chr(13)+Chr(10)+Chr(13)+"<html><header><title>Answer from CGI</title></header><body>Hi, received your data:<br>"
HttpAnswer$+StringField(StringField(Input$, 1, "&"), 1, "=")+": "+StringField(StringField(Input$, 1, "&"), 2, "=")+"<br>"+StringField(StringField(Input$, 2, "&"), 1, "=")+": "+StringField(StringField(Input$, 2, "&"), 2, "=")
HttpAnswer$+"</body></html>"
WriteFile_(hOutput, @HttpAnswer$, Len(HttpAnswer$), @Written, 0)
CloseHandle_(hOutput) 

Posted: Sat Jul 10, 2004 5:03 am
by griz
Thank you very much El_Choni!

This is exactly what I was looking for. :P

Posted: Sun May 22, 2005 9:50 am
by bluejoke
Muchas gracias El_Choni! Thanks for the example, I would never get it right, to pass data from my cgi-app to apache, windows-version, without getting this ***** 500 error.

Hasta luego,
Simon Ruf

Posted: Sun May 22, 2005 1:52 pm
by NoahPhense
Thanks guys.. this will be VERY hand for my server..

- np