Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGI programming Adding Dynamic Content to your Web Site.

Similar presentations


Presentation on theme: "CGI programming Adding Dynamic Content to your Web Site."— Presentation transcript:

1 CGI programming Adding Dynamic Content to your Web Site

2 Typical Browser/Server Interaction Browser (netscape) Server (apache) 1. Broswer requests document 2. Server fetches file from local disk 3. Server returns “html” formatted file to browser 4. Browser renders the html file on the client screen Disk

3 CGI role Browser (netscape) Server (apache) 1. Browser requests document 3. Server returns “html” formatted file to browser 4. Browser renders the html file on the client screen 2a CGI Program 2b 2c 2a. Server invokes CGI 2b. CGI executes and accesses disk (or anything else) as needed 2c. CGI sends “html” to the server Disk

4 Is CGI a language like perl? NO CGI -> Common Gateway Interface CGI is an approach CGI programs can be written with ANY language –perl –c/c++ –shell script –and others!

5 CGI - Input and Output? If a CGI is launched by the web server, where does it get it’s input and where does it write the output? Input –from environment variables –need a library to get the values Output –to standard output! –html statements

6 CGI program with NO input! Written in c #include int main(void) { /* print content type and blank line */ printf("%s%c%c\n", "Content-Type:text/html;charset=iso ”,13,10); printf(” \n”); printf(“ CGI Test \n"); printf(" CGI Test \n"); printf(“ This is my first CGI program!\n”); printf(“ \n”); return 0; } Content-Type:text/html; charset iso CGI TEST CGI Test This is my first CGI program! CGI TEST CGI Test This is my first CGI program! c CGI program CGI Output As seen at browser

7 Same CGI program in c++ #include int main(void) { // print content type and blank line cout << "Content-Type:text/html;charset=iso ” << endl << endl; cout \n”; cout CGI Test \n"; cout This is my first CGI program!\n”; cout \n”; return 0; } Content-Type:text/html; charset iso CGI TEST CGI Test This is my first CGI program! CGI TEST CGI Test This is my first CGI program! c++ CGI program CGI Output As seen at browser These two are the same!

8 Invoking the cgi One way is with a simple

9 What if you have input for the cgi? Assume you have two values to send. Parameters are identified by NAME not position. Call the values x and y GO As before ? separates parameters from the program Two parameters separated from each other by & with name=value format As before

10 How does CGI get the input? Remember the CGI is –on another machine, different from the client –invoked by the server not the browser Learn about unix environment variables –CGI can get it’s values from an environment variable named QUERY_STRING Use a library routine named getenv to get the QUERY_STRING Use sscanf to extract the values of the parameters from the QUERY_STRING 1. 2.

11 C Example getting input #include int main(void) { char *data; long x,y; /* print content type and blank line */ printf("%s%c%c\n", "Content-Type:text/html;charset=iso ”,13,10); printf(” \n”); printf(“ CGI Test \n"); printf(" CGI Test \n"); printf(“ This is my first CGI program!\n”); data = getenv("QUERY_STRING"); if(data == NULL) printf(" Error! Error in passing data from form to script."); else if(sscanf(data,”x=%ld&y=%ld",&x,&y)!=2) printf(" Error! Invalid data. Data must be numeric."); else printf(" The parameters are %ld and %ld is %ld.",x,y); printf(“ \n”); return 0; } c CGI program with input from environment variables Get QUERY_STRING Get x and y from QUERY_STRING

12 CGI input.. REALLY Values are not typically passed in

13 Form example Please specify the inputs: html code Please specify the inputs: Input! Form appearance User input User clicks when ready

14 Other approaches for input Two approaches are available –GET … which is what the previous example used –POST … which is slightly different gets input from stdin not QUERY_STRING Why are two approaches available? –Browsers have history which are easy to reuse. –Users can inadvertently resend the form info, e.g. buy two sets of tickets to the concert. –Browsers will prompt users when reusing POST –Programmers should use GET if repeated use OK –Use POST if submission has long-term effects


Download ppt "CGI programming Adding Dynamic Content to your Web Site."

Similar presentations


Ads by Google