CGI Introduction

27,871 views
27,731 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
27,871
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
44
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

CGI Introduction

  1. 1. Introduction to CGI and PERL CISIWEB 434A
  2. 2. Basic Internet Technology <ul><li>Internet </li></ul><ul><li>IP address </li></ul><ul><li>Domain name </li></ul><ul><li>World Wide Web </li></ul><ul><li>HTML </li></ul><ul><li>Web Server </li></ul><ul><li>Browser </li></ul><ul><li>URL </li></ul><ul><li>Static Web Page Vs. Dynamic Web Page </li></ul>
  3. 3. CGI <ul><li>Common Gateway Interface </li></ul><ul><li>Protocol that allows a server to communicate with CGI scripts </li></ul><ul><li>Stored in a special directory (CGI-BIN) </li></ul><ul><li>Can send information to and receive information from databases on the server </li></ul><ul><li>Creates HTML as output to send to browser window </li></ul><ul><li>PERL language is most widely used language (but not only language) </li></ul>
  4. 4. What is PERL? <ul><li>Practical Extraction and Report Language </li></ul><ul><li>Most often used for creating CGI </li></ul><ul><li>Interesting aside – the language was almost named “gloria” after the wife of Larry Wall, the programs inventor </li></ul>
  5. 5. What do you need? <ul><li>Computer running UNIX or windows </li></ul><ul><li>Browser software </li></ul><ul><li>Perl Interpreter </li></ul><ul><li>A Web server that allows you to create, save, and execute CGI scripts </li></ul><ul><ul><li>Create your own server on your PC by installing apache or IIS </li></ul></ul><ul><ul><li>Use an Internet Service Provider to host your site given that they support CGI </li></ul></ul><ul><ul><ul><li>Example: http://www.netfirms.com </li></ul></ul></ul>
  6. 6. Create the Bin and Set permissions <ul><li>Almost sounds like another language? </li></ul><ul><ul><li>Create a folder to put your files </li></ul></ul><ul><ul><ul><li>Commonly named cgi-bin </li></ul></ul></ul><ul><ul><ul><li>Common gateway interface – binary </li></ul></ul></ul><ul><ul><ul><li>UNIX mkdir cgi-bin </li></ul></ul></ul><ul><ul><li>Chmod should be set to 755 </li></ul></ul><ul><ul><ul><li>Owner can read(4), write(2), execute(1) </li></ul></ul></ul><ul><ul><ul><li>Group and others can read and execute </li></ul></ul></ul><ul><ul><ul><li>UNIX command chmod 755 cgi-bin </li></ul></ul></ul>
  7. 7. How to write a CGI Script <ul><li>First locate perl interpreter </li></ul><ul><ul><li>UNIX – usually /usr/bin/perl </li></ul></ul><ul><ul><li>Windows – C:/Perl/bin/perl.exe </li></ul></ul><ul><li>Use a text editor to create your file – Notepad or wordpad in windows </li></ul><ul><li>Start with shebang line </li></ul><ul><ul><li># ( Sh arp) !(bang symbol) </li></ul></ul><ul><ul><li>Location of the Perl Interpreter </li></ul></ul><ul><ul><li>Ex: #!/usr/bin/perl </li></ul></ul>
  8. 8. Sample CGI code, pg 8 <ul><li>Shebang line </li></ul><ul><li>Comment – starts with a # sign, will be ignored by the interpreter </li></ul><ul><li>Interpreter sends information to the server which will send to the browser so results need to be in proper format for browser (html) </li></ul><ul><ul><li>Content-type header </li></ul></ul><ul><ul><li>Print “Content-type: text/html ”; </li></ul></ul><ul><ul><ul><li>Other types? text/plain or image/gif </li></ul></ul></ul><ul><ul><ul><li>/n/n, ends the line, adds a blank line </li></ul></ul></ul><ul><ul><ul><li>Ends with semi colon, completes the statement </li></ul></ul></ul>
  9. 9. Use Perl to generate html <ul><li>Use Print to create statements with html strings </li></ul><ul><li>Enclose html in quotes </li></ul><ul><li>End statement with semi-colon </li></ul>
  10. 10. Getting the script to work on the server <ul><li>You have to send the script to the server using ftp </li></ul><ul><li>Be sure to send the file as ascii </li></ul><ul><li>Make sure the document is a text file with a cgi extension </li></ul>
  11. 11. Testing a script <ul><li>Testing locally using PERL interpreter </li></ul><ul><ul><li>Find a command prompt </li></ul></ul><ul><ul><li>Change directory to cgi-bin </li></ul></ul><ul><ul><li>Type perl –c file.cgi </li></ul></ul><ul><ul><ul><li>Perl starts the perl interpreter </li></ul></ul></ul><ul><ul><ul><li>-c checks the syntax without executing </li></ul></ul></ul><ul><ul><ul><li>File.cgi is the name of the text script created </li></ul></ul></ul>
  12. 12. Testing a script <ul><li>Testing the file on a server </li></ul><ul><li>Run the file as http://yourservername/cgi-bin/file.cgi </li></ul><ul><li>Press enter – browser submits the url to the server and runs the script which calls the interpreter to convert the instructions to machine code </li></ul><ul><li>View page source, only the HTML is returned as a result of the PERL </li></ul>

×