Download presentation
Presentation is loading. Please wait.
1
CGI, FORMS and Perl CSC 3750 Fall 2012.
2
History The first Web sites consisted of static HTML pages that could not change in response to input from the user. You requested a Web page via your browser, the server received your request, and the server sent back the page that you wanted.
3
History Later, people realized that it was essential to have some interaction between the browser and the server. This led to an extension to Web servers that was called the Common Gateway Interface (CGI).
4
CGI The value of a form is the ability to capture the data that is submitted through the form and have a program on the server do some processing with that data. To be able to capture that data on the server, you must have a Common Gateway Interface (cgi) program, known as a script, ready to execute on your server.
5
CGI Because such scripts have the ability to bypass the security of the computer system, these scripts are usually dealt with great care by the network administrator. To protect the system from breach, a special approach to scripts has been established on the Computer Science Department paris system.
6
CGI cgi-bin Each user is permitted to establish their own directory which will contain their scripts. The name of the directory must be, cgi-bin The permissions must be 705 This directory must be inside your public_html directory. All cgi scripts that you write must be placed inside the cgi- bin directory.
7
CGI directory Use the mkdir command to make this directory.
You can use any of the GUI type ftp (e.g., FileZilla) systems to create such a directory.
8
CGI Script CGI scripts always end with “.cgi” extension
myfirst.cgi Place that script into my folder cgi-bin.
9
CGI format To cause that script to be executed from my web page on someone’s browser, write a line in your web page which might read <A HREF=" d/cgi-bin/myfirst.cgi"> Run my script </A>
10
CGI format Placing such a line in your web page will create a hypertext link with the text “Run my script” as the underlined link. Clicking on that link will send a request to the server asking for the script myfirst.cgi to be executed. Depending on what the script is supposed to do will determine what happens next.
11
CGI another format It is also possible to send data to the script from an anchor element by using the format <A HREF= " bin/myfirst.cgi?argruments"> Run my script </A> The string following the “?” mark is sent to the script on the server to do whatever it wants with that string.
12
FORMS Another method of sending data to the script is the use of FORMS. as follows: <FORM METHOD="GET" ACTION=" <INPUT TYPE="TEXT" NAME="theName"> Enter your Name <P><INPUT TYPE="SUBMIT"> </FORM>
13
FORMS This form would cause the following image to appear on your web page. When the “Submit Query” button is pressed, whatever is typed in the text box would then be sent to the server script specified in the ACTION parameter.
14
Scripts A script must send an entire, properly formatted HTML web page back to your browser for your browser to display it. This means the script must contain steps that “print” all of the HTML and text associated with an entire web page.
15
Perl Scripts Scripts can be written in a number of computer languages (e.g., C++, java, and perl). Perl is the most popular. Perl was developed in 1986 and has evolved over the years. It is available for almost all computers and the best part is, it is free. It is an interpretive language and does not produce a compiled version of your script.
16
Perl ex 1) An example in perl.
The following is a perl script that will just send back to the browser a simple web page.
17
Perl Script #!/usr/bin/perl # a sample cgi program
print <<theEND; Content-type: text/html ( There must be a blank line left here. Remove this line if you copy/paste from here.) <HTML> <HEAD> <TITLE>perl example</TITLE> </HEAD> <BODY> <H1 > <FONT COLOR=”orange”> Here is a line of large text sent back to the browser from the server. It is from a perl script. </FONT> </H1> </BODY> </HTML> theEND
18
Perl Script: NOTE In the line "print <<theEND; " there must be a space between the word print and the << and there can be no space between the << and the word theEND. Also theEND is case sensitive, so you must type it exactly as shown. And there is no semicolon after the very last, theEND.
19
Perl Script The perl script must be created in a text editor, then saved as a file named like, myfirst.cgi. It would be uploaded into cgi-bin directory. Now, make sure that the script work, i.e. download from the server to the browser.
20
Perl Script Put a web page on the server that had a line in the web page something like: <A HREF= " bin/myfirst.cgi"> Run my script </A> Then start up your web page and click on the hot link Run my script so you can see what perl script can do.
21
Response (myfirst.cgi)
What you would see next in your browser window would be a web page that looks like:
22
2nd Example 2) A second example in perl.
The following is a perl script that will accept data sent from a FORM and simply send back to the browser what was typed in the FORM.
23
2nd Example #!/usr/bin/perl # a sample cgi program # (this next line reads the data sent by the FORM) $textline = $ENV{'QUERY_STRING'}; print <<theEND; Content-type: text/html ( There must be a blank line left here.) <HTML> <HEAD> <TITLE>perl example</TITLE> </HEAD> <BODY> Here is what you sent to the server: <H3> $textline </H3> </BODY> </HTML> theEND
24
CGI Redirection CGI scripts can return a complete html document like the examples above, or it can send back a reference to another document that is to be loaded. Instead of sending back a full html document, the script can just tell the browser where to get the new one.
25
CGI Redirection For example, suppose your script is written such that it may determine from the text received that it wants to send the client to some other web site. Suppose the web site were at the location The script would only need to send back to the browser the 2 lines: Content-type: text/html Location:
26
CGI Redirection (Example)
#!/usr/bin/perl # a sample cgi program print <<theEND; Content-type: text/html Location: theEND
27
Simple functions A simple function that can be used in perl programs is the one that allows you to access the local date and time from the computer of the server. This is done by invoking the function, localtime(time)
28
Simple functions A variable, say $time, could be set to the value this function extracts from the server system, as follows: $time=localtime(time); This statement places the value of the local date and time into the variable $time. (Any variable name could have been used.) In a perl program, this might be used as shown in the following example:
29
Functions Example #!/usr/bin/perl # a sample cgi program $time=localtime(time); print <<theEND; Content-type: text/html ( There must be a blank line left here.) <HTML> <HEAD> <TITLE>perl example 2</TITLE> </HEAD> <BODY> <P> <FONT COLOR="red"> You have just reached my web site in Detroit, Michigan. <P> The local time here is $time </FONT> </BODY> </HTML> theEND
30
Simple functions If you want the background color to appear in black with the text as white, change the code as follows: #!/usr/bin/perl # a sample cgi program $time=localtime(time); print <<theEND; Content-type: text/html <HTML> <HEAD> <TITLE>perl example 3</TITLE> </HEAD> <BODY BGCOLOR="black"> <P> <FONT COLOR="white"> You have just reached my web site in Detroit, Michigan. <P> The local time here is $time </FONT> </BODY> </HTML> theEND
31
Permissions Before you upload the script file, be certain you set the type to ASCII or TEXT. Also, be certain the permissions on your file in the cgi-bin directory are set to Paris automatically sets permissions to files at This will not permit the cgi script to run. It must be 705 to work. Also be sure the permissions on your cgi- bin directory is 705.
32
Debugging CGI Scripts You can test your cgi script directly on paris to see if it works. You can run the script from the unix command line by just typing its path name. Or, once the cgi-bin is your working directory, just type the name of the script at the command line and it will run. If your script does not have syntax errors, when it runs on paris, it will simply print to your screen the stream of HTML lines it would have sent to the client browser. If your script runs without errors, you only need to be concerned that it does what you expect it to do. If not, you must fix the problem using your text editor (or pico) and uploading the file again to the cgi-bin directory before proceeding. If typing the script name fails, try typing ./scriptname.cgi next to the command prompt.
33
Test your cgi script: telnet to your account get to the cgi-bin
paris%cd public_html/cgi-bin paris%ls –l first.cgi paris%first.cgi
34
Test your cgi script: If your cgi script works properly, this command will result in your HTML code being printed to your screen. If instead you get an error message, your perl code has some error which you need to correct.
35
Debugging CGI Scripts After your script does what you expect, then try running it from your web page. You can actually test your script by simply typing into the location bar paris.cs.wayne.edu/~yourID/cgi- bin/scriptname.cgi?John and press the Return key. If your script will not run or does not run without error, you must fix the errors before proceeding.
36
When Script Fails: step 1
Make sure that the permissions on your script file is Most uploaded files seem to be set at 664 by the system and that will not allow a cgi script to work. Also be sure the permissions on your cgi- bin directory is 705.
37
When Script Fails: step 2
Make sure that your file is a plain ASCII file. If you have developed the script file on your PC or Mac and used some ftp client to upload it to the server, make sure that it does not have a carriage return (^M) at the end of each line. These are control codes inserted by some text editing packages that you cannot see, but perl can see these ^M codes, and does not like them in the files.
38
When Script Fails: step 2
To view your file to see these invisible codes, you can use the pico editor. At the paris command line next to the prompt type pico nameofscript
39
When Script Fails: step 2
This will show you the lines in your script and you will see whether there are any carriage returns (^M) in the file. You can use the arrow keys to move around in the file and use the "delete" key to remove these unwanted characters. To get out of pico, you need to type ^x ( Control key and a 'x') and enter a "y" if you want the changes to take effect.
40
When Script Fails: step 2
If you find that your file does have carriage returns (^M) at the end of any lines, you probably used ftp's binary transfer mode. If you do not want to use pico on paris to fix these, then on your computer set the ftp transfer mode to ASCII ( or text) before you upload the file and upload it again.
41
When Script Fails: step 3
If your file in pico is meaningless gibberish with the name of your PC application embedded in it, go back and save the file on your PC as a plain text (.txt) file and upload it again, but make sure you upload it choosing the text or ASCII file option first.
42
When Script Fails: step 4
On occasion, the blank line that has to be inserted after the "Content-type: text/html" line seems to get corrupted in a way you cannot see, but perl does not like. If all else looks OK to you and you think this may be the problem, use pico to delete the blank line that is there, and use the "Return" key to enter a new blank line. This should correct that problem.
43
When Script Fails: step 5 & 6
5) If you are still getting an error notice, on paris look in /var/log/httpd/error_log (use the tail(1) command) for further information. 6) When you have exhausted all other debugging options, contact me first and then the web server administrator.
44
Permission Summary For normal web site operation, the paris settings should be: 1) public_html and cgi-bin directories must be at 705 2) all web pages and images must be at 604 (or 704) 3) all cgi scripts must be at 705
45
Permission Summary If you are concerned about other people copying your files, you should set your public_html directory permission to 700 until you are ready to test your website. Change the permission back to 705, test your site, and change the permission back to On the due date of the project change the permissions to 705 and leave it there.
46
Extracting the Text The string from a FORM element has the format
"name=data-entered-in-text-box" If you add the following two lines after the "ENV" statement in the perl script, the CGI script will extract only the portion of the string following the = sign. The part called "data-entered-in-text-box" will be placed into a variable called "$thetext". @frombox=split(/=/,$textline); $thetext=$frombox[1]; You could then use that variable in your HTML part to print only the name.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.