Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adding Dynamic Content to your Web Site

Similar presentations


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

1 Adding Dynamic Content to your Web Site
CGI programming Adding Dynamic Content to your Web Site

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

3 CGI role Server (apache) Browser (netscape) 2a 2c CGI Program
1. Browser requests document Server (apache) Browser (netscape) 3. Server returns “html” formatted file to browser 2a 2c 4. Browser renders the html file on the client screen CGI Program 2a. Server invokes CGI 2b. CGI executes and accesses disk (or anything else) as needed 2c. CGI sends “html” to the server 2b 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
c CGI program CGI Output #include <stdio.h> #include <stdlib.h> int main(void) { /* print content type and blank line */ printf("%s%c%c\n", "Content-Type:text/html;charset=iso ” ,13,10); printf(”<html>\n”); printf(“<TITLE>CGI Test</TITLE>\n"); printf("<H3>CGI Test</H3>\n"); printf(“<p>This is my first CGI program!\n”); printf(“</html>\n”); return 0; } Content-Type:text/html; charset iso <html> <TITLE>CGI TEST</TITLE> <H3>CGI Test</H3> <p>This is my first CGI program! </html> As seen at browser CGI TEST CGI Test This is my first CGI program!

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

8 Invoking the cgi One way is with a simple <a href=…
The program must be in a directory from which the server allows cgi programs to be executed typically the program ends in .cgi and is executable example: <a href=“/cgi-bin/thiscgi.cgi”>GO</a>

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 <a href=“/cgi-bin/thiscgi.cgi?x=4&y=7”>GO</a> As before As before Two parameters separated from each other by & with name=value format ? separates parameters from the program

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
c CGI program with input from environment variables #include <stdio.h> #include <stdlib.h> 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(”<html>\n”); printf(“<TITLE>CGI Test</TITLE>\n"); printf("<H3>CGI Test</H3>\n"); printf(“<p>This is my first CGI program!\n”); data = getenv("QUERY_STRING"); if(data == NULL) printf("<P>Error! Error in passing data from form to script."); else if(sscanf(data,”x=%ld&y=%ld",&x,&y)!=2) printf("<P>Error! Invalid data. Data must be numeric."); else printf("<P>The parameters are %ld and %ld is %ld.",x,y); printf(“</html>\n”); return 0; } Get QUERY_STRING Get x and y from QUERY_STRING

12 CGI input .. REALLY Values are not typically passed in <a href=..
We use input to VARY the values Need to give the user/client an opportunity to vary the parameters This is done with a form define form fields with a name for each field define a submit button associated with a cgi program and those parameters the submit takes the place of the href

13 Form example html code Form appearance Please specify the inputs:
<FORM ACTION=" <P>Please specify the inputs: <INPUT NAME=”x" SIZE="5"> <INPUT NAME=”y" SIZE="5"><BR> <INPUT TYPE="SUBMIT" VALUE=”Input!"> </FORM> Form appearance Please specify the inputs: Input! 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 "Adding Dynamic Content to your Web Site"

Similar presentations


Ads by Google