0blivion1:(CGI-FAQ.txt):15/03/2000 << Back To 0blivion1


+------------------------------------------------------+ ▌ Oblivion Underground Magazine - Issue 1 - 15/03/2000 ▌ ▌ CGI - Frequently Asked Questions by Cyber0ptix ▌ ▌ E-Mail : cyberoptix@email.com ▌ +------------------------------------------------------+ Common Gateway Interface - Frequently Asked Questions ----------------------------------------------------- Q) What is CGI? A) CGI stands for the Common Gateway Interface, a method of connecting server based programs to the world wide web. On the server that you host your web site, if it has CGI enabled, you will have a special directory from which you can run such scripts. To access the server you will have to use FTP or TELNET software. When you connect to your server you will usually start in your home directory. You will find another directory in here called 'cgi-bin'. To keep things simple, put all your scripts into the 'cgi-bin' directory (you can make sub-directorys if you want), and any HTML, GIF or JPG files into your root directory. Q) What is PERL and how is it related to CGI? A) PERL and CGI are not intrinsically linked. PERL is a scripting language that provides some excellent text manipulation features, and because of this it has become a powerfull weapon in the arsenal of a CGI programmer. It is feasible to write CGI scripts in almost any language (including Visual Basic, *NIX Shell Scripts and Even MSDOS .Bat Files!!). PERL is a very portable language, and for those who want to test their scripts on a local machine a Win32 (NT/9x) port is available from www.activestate.com. This FAQ will concentrate on scripting using PERL. Q) What is the funny looking thing (#!/usr/bin/perl) at the start of a PERL script? A) As a PERL script is not compiled, when it is accessed just by using its name, the operating system needs to know the location of the PERL interpreter so that the script can be executed. The #! or Shebang as it is called provides the location of the PERL interpreter. Q) What is a HTTP Header? A) As your script will usually return a document to the client (web browser) you have to make sure that you send a correct header to make sure that the client knows how to handle your document. For example if you are outputting an HTML document you should output, at least, the following information. Content-type: text/html This should be followed by a blank line (this signifies the end of the header). So in PERL you would use: print "Content-type: text/html"; If you wanted to output a HTML document, or text/plain if you were outputting a textfile. You can also generate GIF and Postscript files, and send those to the browser, but ill not be covering that in the FAQ. You should take care to output this line as early in your script as possible, as if any thing is output before this header the server will produce an error. There are other lines that make up a complete header, but if you just add the content type the server will fill in the blanks for you. There is a situation where you may wish to use a complete header. Please refer to the section on non-parsed headers. Q) Ive written a script, what do I do now? A) Once you have written a script it is a good idea to test it locally, and see if it is going to give the output you expected, there are all sorts of command line switches you can use to enfore extra checking but a simple: perl -w script.name should warn you of any serious syntax errors. If everything is OK your ready to upload the script to your server. All distributions of PERL have this feature, so you can use the '-w' switch on your server to test any scripts that you have already uploaded. Q) How do I upload my scripts to my CGI Server? A) To upload your scripts to your server simply FTP to your web server. Then change into your 'cgi-bin' directory and send your PERL file. Using the DOS ftp command the command is as follows c:/>ftp cgiserver.com Connected cgiserver.com username: yourusername password: yourpassword >send localfilename remotefilename When uploading scripts make sure that you upload them as ASCII!! This is because text files in UNIX/Linux end each line with a Carriage Return / Linefeed Pair <CR/LF> HINT: Anyone using a text file as a data source, in a one record per line format, will need to watch for this as well. Q) I've uploaded my script, what now? A) Once you have uploaded your script you need to change its file permissions to make it executable by the server. You can do this by using TELNET to connect to the server and from within the 'cgi-bin' directory typing: chmod 755 myfile.pl (Obviously replace the filename!) Or if you prefer you can use your FTP software. Q) What do the numbers mean? A) 755 refers to the file permissions on the file. IT allows only the owner of the file to Read/Write/Execute and everyone else have /Read/Execute rights so it will be allowed to run. Q) How do I access my script from the browser? A) You should just point your browser at: servername.com/cgi-bin/myfile.pl This is the location for hyperlinks in other HTML documents aswell. That is the end of this simple introduction to CGI scripts and how to use them, hope you all enjoyed it.... Cyber0ptix