CGICGI ScriptsScripts GETGET
n FormForm datadata formsforms queryquery stringstring andand isis appendedappended toto thethe URLURL ofof thethe scriptscript – Can be accessed by using the environment variable QUERY_STRING – Browser or server may truncate data that exceeds an arbitrary number of characters
CGI Scripts 2 GETGET
n GETGET requestrequest byby clientclient GET /cgi-bin/prog.pl?name= Bill+age=29 HTTP/1.0 Accept: www/source Accept: text/html Accept: text/plain User-Agent: Lynx/2.4 libwww/2.14
CGI Scripts 3 GETGET
n CanCan accessaccess CGICGI programprogram withwith aa queryquery withoutwithout usingusing aa formform CGI program n Can also send extra path information – Usually file name CGI program
CGI Scripts 4 POSTPOST
n QueryQuery lengthlength isis unlimitedunlimited n ScriptScript readsreads fromfrom standardstandard inputinput n Can'tCan't createcreate cannedcanned queriesqueries
CGI Scripts 5 POSTPOST
POST /cgi-bin/prog.pl HTTP/1.0 Accept: www/source Accept: text/html Accept: text/plain User-Agent: Lynx/2.4 libww/2.14 Content-type: application/x-www- form-urlencoded Content-length: 22
user=Larry+Bird&age=30 CGI Scripts user=Larry+Bird&age=30 6 DecodingDecoding FormForm DataData
n DetermineDetermine requestrequest protocolprotocol byby checkingchecking thethe REQUEST_METHODREQUEST_METHOD environmentenvironment variablevariable – GET or POST – One script can work for both methods n IfIf thethe protocolprotocol isis GET,GET, readread queryquery stringstring fromfrom QUERY_STRINGQUERY_STRING and/orand/or thethe extraextra pathpath informationinformation fromfrom PATH_INFOPATH_INFO
CGI Scripts 7 DecodingDecoding FormForm DataData
n IfIf thethe protocolprotocol isis POST,POST, determinedetermine thethe sizesize ofof thethe requestrequest usingusing CONTENT_LENGTHCONTENT_LENGTH andand readread thatthat amountamount ofof datadata fromfrom thethe standardstandard inputinput n SplitSplit thethe queryquery stringstring onon thethe '&''&' character,character, whichwhich separatesseparates keykey--valuevalue pairspairs – The format is key=value&key=value...
CGI Scripts 8 DecodingDecoding FormForm DataData
n DecodeDecode thethe hexadecimalhexadecimal andand '+''+' characterscharacters inin eacheach keykey--valuevalue pairpair n CreateCreate aa keykey--valuevalue tabletable withwith thethe keykey asas thethe indexindex
CGI Scripts 9 ExamplesExamples
n UsingUsing formsforms withwith POSTPOST – Plain text n GuestbookGuestbook – http://jupiter.cs.wayne.edu/~grosky /user-cgi/guestbook.pl?add using the GET method, will present a form for you to enter information
CGI Scripts 10 ExamplesExamples
n GuestbookGuestbook – http://jupiter.cs.wayne.edu/~grosky /user-cgi/guestbook.pl using the GET method, will display the actual guestbook file [guestbook.htm] – Can open guestbook file directly by accessing http://jupiter.cs.wayne.edu/ ~grosky/CSC691/CGIScripts/ Guestbook/guestbook.htm
CGI Scripts 11 ExamplesExamples
n GuestbookGuestbook – When form is submitted using the POST method, this program decodes the information and outputs a thank-you message
CGI Scripts 12 ExamplesExamples
n SurveySurvey – http://jupiter.cs.wayne.edu/~grosky /user-cgi/shopping.pl/start.htm – http://jupiter.cs.wayne.edu/ ~grosky/CSC691/CGIScripts/Hidden Fields/start.htm
CGI Scripts 13