home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / www / cgi-faq < prev    next >
Encoding:
Internet Message Format  |  2000-07-24  |  61.8 KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!howland.erols.net!dispose.news.demon.net!demon!news.demon.co.uk!demon!hyperlens.demon.co.uk!sleipnir.webthing.com!nobody
  2. From: Nick Kew <nick@webthing.com>
  3. Newsgroups: comp.infosystems.www.authoring.cgi,comp.answers,news.answers
  4. Subject: FAQ: Frequently Asked Questions about CGI Programming
  5. Supersedes: <cgi-faq-956193977@jarl.webthing.com>
  6. Followup-To: comp.infosystems.www.authoring.cgi
  7. Date: 23 Jul 2000 09:57:29 GMT
  8. Organization: WDG
  9. Approved: news-answers-request@MIT.EDU
  10. Expires: 12 Aug 2000 09:57:29 GMT
  11. Message-ID: <cgi-faq-964346249@jarl.webthing.com>
  12. NNTP-Posting-Host: hyperlens.demon.co.uk
  13. Summary: The Common Gateway Interface - Programming for the WWWeb:
  14.     Basics (what is CGI; when to use CGI vs other
  15.         web programming techniques)
  16.     HTTP and NPH scripts: technical info and references
  17.     Programming tips: "How do I do this..."
  18.     Troubleshooting: How to tackle your problems
  19.     Further Reading: related FAQs and reference material
  20. Keywords: CGI,FAQ,HTTP,WWW
  21. X-NNTP-Posting-Host: hyperlens.demon.co.uk:212.228.207.125
  22. X-Trace: news.demon.co.uk 964355428 nnrp-09:2415 NO-IDENT hyperlens.demon.co.uk:212.228.207.125
  23. X-Complaints-To: abuse@demon.net
  24. Content-Type: text/plain; charset=us-ascii
  25. Mime-Version: 1.0
  26. Lines: 1646
  27. Xref: senator-bedfellow.mit.edu comp.infosystems.www.authoring.cgi:84553 comp.answers:41642 news.answers:188270
  28.  
  29. Archive-Name: www/cgi-faq
  30. Posting-Frequency: Irregular
  31.  
  32. Frequently Asked Questions on CGI Programming
  33. ---------------------------------------------
  34.  
  35. 0.   Preamble
  36. 0.1. Changes
  37. 0.2. Notice and Disclaimer
  38. 0.3. Where to get this document
  39. 0.4. How to contribute to this document?
  40. 0.5. Can I email the author my questions?
  41. 0.6. What's up with posting to comp.infosystems.www.authoring.cgi?
  42. 0.7. Credits
  43.  
  44. 1.   Basic Questions
  45. 1.1. What is CGI?
  46. 1.2. Is it a script or a program?
  47. 1.3. When do I need to use CGI?
  48. 1.4. Should I use CGI or JAVA?
  49. 1.5. Should I use CGI or SSI or ... { PHP/ASP/... }
  50. 1.6. Should I use CGI or an API?
  51. 1.7. So what are in a nutshell the options for webserver programming?
  52. 1.8. What do I absolutely need to know?
  53. 1.9. Does CGI create new security risks?
  54. 1.10. Do I need to be on Unix?
  55. 1.11. Do I have to use Perl?
  56. 1.12. What languages should I know/use?
  57. 1.13. Do I have to put it in cgi-bin?
  58. 1.14. Do I have to call it *.cgi?  *.pl?
  59. 1.15. What is the "CGI Overhead", and should I be worried about it?
  60. 1.16. What do I need to know about file permissions and "chmod"?
  61. 1.17. What is CGIWrap, and how does it affect my program?
  62. 1.18. How do I decode the data in my Form?
  63.  
  64. 2.   HTTP Headers and NPH Scripts
  65. 2.1. What is HTTP (HyperText Transfer Protocol)?
  66. 2.2. What HTTP request headers can I use?
  67. 2.3. What Environment variables are available to my application?
  68. 2.4. Why doesn't my script get REMOTE_USER?  My page is password-protected.
  69. 2.5. What HTTP response headers do I need to know about?
  70. 2.6. What is NPH?
  71. 2.7. Must/should/can I write nph scripts?
  72. 2.8. Do I have to call it nph-*
  73. 2.9. What is the difference between GET and POST?
  74.  
  75. 3.   Techniques: "How do I..."
  76. 3.1. Can I get information about who is visiting?
  77. 3.2. Can I get the email of visitors?
  78. 3.3.     "But I saw some.kool.site display my email address..."
  79. 3.4. Can I verify the email addresses people enter in my Form?
  80. 3.5. Subject: How can I get the hostname of the remote user?
  81. 3.6. Can I get browser details and return different pages?
  82. 3.7. Can I trace where a user has come from/is going to?
  83. 3.8. Can I launch a long process and return a page before it's finished?
  84. 3.9. Can I launch a long process which the user interacts with?
  85. 3.10. Can I password-protect my pages?
  86. 3.11. Can I do HTTP authentication using CGI?
  87. 3.12. Can I identify users/sessions without password protection?
  88. 3.13. Can I redirect users to another page?
  89. 3.14. Can I run a CGI script without returning a new page to the browser?
  90. 3.15. Can I write output to a different Netscape frame?
  91. 3.16. Can I write output to several frames at once?
  92. 3.17. Can I use a CGI script to generate both text and inline images?
  93. 3.18. How can I use Caches to make CGI scripts faster and more Net-friendly?
  94. 3.19. How can I avoid users hitting "submit" twice?
  95. 3.20. How can I stop my CGI script reading and writing files as "nobody"?
  96. 3.21. How can I prevent my CGI results being cached by the browser?
  97. 3.22. How can I control the default filename when downloading a file via CGI?
  98.  
  99. 4.   Troubleshooting a CGI application
  100. 4.1. Are there some interactive debugging tools and services available?
  101. 4.2. I'm having trouble with my headers.   What can I do?
  102. 4.3. Why do I get Error 500 ("the script misbehaved", or "Internal Server Error")
  103. 4.4. I tried to use (Content-Type|Location|whatever), but it appears in my Browser?
  104. 4.5. How can I run my CGI program 'live' in a debugger?
  105. 4.6. I'm using CGI with QUERY_STRING embedded in my HTML, but it gets corrupted?
  106.  
  107. 5.   Further Reading
  108. 5.1. Other FAQs/collections
  109. 5.2. Reference Pages
  110.  
  111. INDEX
  112.  
  113. -------------------------------------------------------------
  114.  
  115. Subject: SECTION 0 -   PREAMBLE
  116.  
  117. NOTE: the numbering in this document is automatically generated by my
  118. posting software, and will change between postings if new questions are
  119. added (as _may_ happen when I see - or someone contributes - a FAQ I've
  120. previously overlooked :-)
  121.  
  122.  
  123. ------------------------------
  124.  
  125. Subject: 0.1 Changes
  126.  
  127.  
  128. Last Modified: July 2000.  Updated several links reported
  129. by Site Valet as moved.  Otherwise unchanged.
  130.  
  131.  
  132. ------------------------------
  133.  
  134. Subject: 0.2 Notice and Disclaimer
  135.  
  136.  
  137. Copyright 1996-2000 Nick Kew.
  138.  
  139. You are free to copy or distribute this document in whole or in part
  140. for any purpose and on any medium you choose, provided you include
  141. this notice and disclaimer in full.
  142.  
  143. Disclaimer: This information is offered in good faith and in the hope
  144. that it may be of use, but is not guaranteed to be correct, up to date
  145. or suitable for any particular purpose.   The author accepts no liability
  146. in respect of this information or its use.
  147.  
  148.  
  149. ------------------------------
  150.  
  151. Subject: 0.3 Where to get this document
  152.  
  153.  
  154. The official homes of this document on the Web are now
  155.     URL  http://www.webthing.com/tutorials/cgifaq.html
  156.     URL  http://www.htmlhelp.org/faq/cgifaq.html
  157.  
  158. NOTE - If you want to mirror the FAQ on your WWW site on a
  159. publicly-visible server, please make sure you keep it up-to-date.
  160.  
  161. Other known sources are:
  162.  
  163. (1) USENET: posted to newsgroups                (TEXT)
  164.     news:comp.infosystems.www.authoring.cgi
  165.     news:comp.answers
  166.     news:news.answers
  167.  
  168. (2) RTFM and mirror sites                    (TEXT)
  169.     ftp://rtfm.mit.edu/pub/usenet/news.answers/www/cgi-faq
  170.  
  171. (3) RTFM WWW mirror sites, including            (Partial HTML)
  172.     Europe - http://www.cs.ruu.nl/cgi-bin/faqwais 
  173.     America - http://www.cis.ohio-state.edu/hypertext/faq/usenet/
  174.  
  175. (4) By EMAIL from the FAQserver at RTFM             (TEXT)
  176.     Send email to mailto:mail-server@rtfm.mit.edu with
  177.         send usenet/news.answers/www/cgi-faq
  178.     in the body of your message
  179.  
  180.  
  181. ------------------------------
  182.  
  183. Subject: 0.4 How to contribute to this document?
  184.  
  185.  
  186. I have removed the InterFAQ from this answer, as it has become
  187. clear that people prefer the familiar approach of emailing me
  188. to that of contributing via the web, and (in turn) the InterFAQ
  189. contents has not been maintained for some time.  Thomas Boutell
  190. has since introduced a somewhat similar project, the OpenFAQ.
  191.  
  192. Just mail me.  ( mailto:nick@webthing.com )
  193.  
  194.  
  195. ------------------------------
  196.  
  197. Subject: 0.5 Can I email the author my questions?
  198.  
  199.  
  200. Please don't.  Post them to an appropriate newsgroup, where they'll
  201. be seen and possibly answered by a whole lot more people than just me.
  202. And remember: bad (or incoherent) questions get bad answers, so think
  203. carefully before posting.
  204.  
  205. If you have an actual programming job to do, I might be interested
  206. However, I am unlikely to be interested in jobs below $1000.
  207.  
  208. If you think something already in the FAQ needs clarifying, feel free
  209. to mail me: don't expect a personal reply, but I *might* add
  210. something to the answer in question, so check the next posting (or three).
  211.  
  212.  
  213. ------------------------------
  214.  
  215. Subject: 0.6 What's up with posting to comp.infosystems.www.authoring.cgi?
  216.  
  217.  
  218. This is now a moderated newsgroup.   The moderator is a bot run by
  219. Thomas Boutell ( mailto:boutell@boutell.com ).   The charter for
  220. moderation is as follows:
  221.  
  222.   This newsgroup is self-moderated.  Your first posting will not appear
  223.   until you have read and responded to an automatic welcome mailing, at
  224.   which point your posting will appear with no further delay.  Provision
  225.   will also be made to automatically approve first postings that contain
  226.   a header requesting this.  Subsequent postings are approved
  227.   automatically.
  228.  
  229. If posting normally doesn't work - as could be the case if your
  230. newsfeed has trouble with moderated groups - you can post articles
  231. by emailing them to:
  232.     mailto:authoring-cgi@boutell.com
  233. Provided the return address in your mail is correct, you will then
  234. receive precise instructions for having your post(s) automatically approved.
  235.  
  236. Alternative means of posting are detailed in the WWW FAQ, posted
  237. regularly by Thomas Boutell.
  238.  
  239.  
  240. ------------------------------
  241.  
  242. Subject: 0.7 Credits
  243.  
  244.  
  245. This FAQ was written by Nick Kew, and has been considerably improved
  246. with the help of comments and criticisms, newsgroup posts and
  247. miscellaneous suggestions from correspondents including
  248. Nathan Neulinger, Maurice L. Marvin, Matthew Healy, Alan J. Flavell,
  249. Don Libes, Alain Deckers, David S. Jackson, J.M. Ivler, and no doubt
  250. others I've forgotten to credit (please remind me if necessary).
  251.  
  252.  
  253. -------------------------------------------------------------
  254.  
  255. Subject: SECTION 1 -   BASIC QUESTIONS
  256.  
  257. This section aims to deal with basic questions, addressing the role and
  258. nature of CGI, and its place in Web programming. Questions/answers which
  259. just don't appear to 'fit' under any other section may also be included
  260. here.
  261.  
  262.  
  263. ------------------------------
  264.  
  265. Subject: 1.1 What is CGI?
  266.  
  267.  
  268. [ from the CGI reference http://hoohoo.ncsa.uiuc.edu/cgi/overview.html ]
  269.  
  270. The Common Gateway Interface, or CGI, is a standard for external
  271. gateway programs to interface with information servers such as HTTP servers.
  272. A plain HTML document that the Web daemon retrieves is static,
  273. which means it exists in a constant state: a text file that doesn't change.
  274. A CGI program, on the other hand, is executed in real-time, so that it
  275. can output dynamic information.
  276.  
  277.  
  278. ------------------------------
  279.  
  280. Subject: 1.2 Is it a script or a program?
  281.  
  282.  
  283. The distinction is semantic.   Traditionally, compiled executables
  284. (binaries) are called programs, and interpreted programs are usually
  285. called scripts.   In the context of CGI, the distinction has become
  286. even more blurred than before.   The words are often used interchangably
  287. (including in this document).   Current usage favours the word "scripts"
  288. for CGI programs.
  289.  
  290.  
  291. ------------------------------
  292.  
  293. Subject: 1.3 When do I need to use CGI?
  294.  
  295.  
  296. There are innumerable caveats to this answer, but basically any
  297. Webpage containing a form will require a CGI script or program
  298. to process the form inputs.
  299.  
  300.  
  301. ------------------------------
  302.  
  303. Subject: 1.4 Should I use CGI or JAVA?
  304.  
  305.  
  306. [answer to this non-question hopes to try and reduce the noise level of
  307. the recurrent "CGI vs JAVA" threads].
  308.  
  309. CGI and JAVA are fundamentally different, and for most applications
  310. are NOT interchangable.
  311.  
  312. CGI is a protocol for running programs on a WWW server.  Whilst JAVA
  313. can also be used for that, and even has a standardised API (the servlet,
  314. which is indeed an alternative to CGI), the major role of JAVA on the
  315. Web is for clientside programming (the applet).
  316.  
  317. In certain instances the two may be combined in a single application:
  318. for example a JAVA applet to define a region of interest from a
  319. geographical map, together with a CGI script to process a query
  320. for the area defined.
  321.  
  322.  
  323. ------------------------------
  324.  
  325. Subject: 1.5 Should I use CGI or SSI or ... { PHP/ASP/... }
  326.  
  327.  
  328. CGI and SSI (Server-Side Includes) are often interchangable, and it may
  329. be no more than a matter of personal preference.   Here are a few
  330. guidelines:
  331.   1) CGI is a common standard agreed and supported by all major HTTPDs.
  332.      SSI is NOT a common standard, but an innovation of NCSA's HTTPD
  333.      which has been widely adopted in later servers.   CGI has the
  334.      greatest portability, if this is an issue.
  335.   2) If your requirement is sufficiently simple that it can be done
  336.      by SSI without invoking an exec, then SSI will probably be
  337.      more efficient.   A typical application would be to include
  338.      sitewide 'house styles', such as toolbars, netscapeised <body>
  339.      tags or embedded CSS stylesheets.
  340.   3) For more complex applications - like processing a form -
  341.      where you need to exec (run) a program in any case, CGI
  342.      is usually the best choice.
  343.   4) If your transaction returns a response that is not an HTML page,
  344.      SSI is not an option at all.
  345.  
  346. Many more recent variants on the theme of SSI are now available.
  347. Probably the best-known are PHP which embeds server-side scripting
  348. in a pre-html page, and ASP which is Microsoft's version of a
  349. similar interface.
  350.  
  351.  
  352. ------------------------------
  353.  
  354. Subject: 1.6 Should I use CGI or an API?
  355.  
  356.  
  357. APIs are proprietary programming interfaces supported by particular
  358. platforms.   By using an API, you lose all portability.   If you know
  359. your application will only ever run on one platform (OS and HTTPD),
  360. and it has a suitable API, go ahead and use it.   Otherwise stick to CGI.
  361.  
  362.  
  363. ------------------------------
  364.  
  365. Subject: 1.7 So what are in a nutshell the options for webserver programming?
  366.  
  367.  
  368. Too many to enumerate - but I'll try and summarise.  Briefly, there
  369. are several decisions you have to make, including:
  370.   * Power.  Is it up to a complex task?
  371.   * Complexity.  How much programming manpower is it worth?
  372.   * Portability.  Might you want to run your program on another system?
  373.  
  374. So here's an overview of the main options.  It's inevitably subjective,
  375. but may be helpful to someone:
  376.  
  377. Basic SSI:        Simple interface for basic dynamic content.
  378.             Non-standard - read your server docs.
  379. Enhanced SSI[1]:    Suitable for more complex tasks within
  380.             an HTML page.
  381. CGI:            The standardised, portable general-purpose API,
  382.             not limited to working with HTML pages.
  383. Enhanced CGI-like[2]:    Typically gain efficiency but lose portability
  384.             compared to standard CGI.
  385. Servlets:        An alternative API for JAVA, that overcomes
  386.             the limitation of JAVA not supporting
  387.             environment variables.
  388. Server API:        Generally the most powerful and most complex option.
  389.  
  390. [1] For example, PHP, ASP.
  391. [2] For example, CGI adapted to mod_perl or fastcgi.
  392.  
  393.  
  394. ------------------------------
  395.  
  396. Subject: 1.8 What do I absolutely need to know?
  397.  
  398.  
  399. If you're already a programmer, CGI is extremely straightforward, and just
  400. three resources should get you up to speed in the time it takes to read them:
  401.   1) Installation notes for your HTTPD.   Is it configured to run CGI
  402.      scripts, and if so how does it identify that a URL should be executed?
  403.      (Check your manuals, READMEs, ISP webpages/FAQS, and if you still can't
  404.      find it ask your server administrator).
  405.   2) The CGI specification at NCSA tells you all you need to know
  406.      to get your programs running as CGI applications.
  407.      http://hoohoo.ncsa.uiuc.edu/cgi/interface.html
  408.   3) WWW Security FAQ.   This is not required to 'get it working', but
  409.      is essential reading if you want to KEEP it working!
  410.      http://www.w3.org/Security/Faq/www-security-faq.html
  411.  
  412. If you're NOT already a programmer, you'll have to learn.   If you would
  413. find it hard to write, say, a 'grep' or 'cat' utility to run from the
  414. commandline, then you will probably have a hard time with CGI.   Make
  415. sure your programs work from the commandline BEFORE trying them with CGI,
  416. so that at least one possible source of errors has been dealt with.
  417.  
  418.  
  419. ------------------------------
  420.  
  421. Subject: 1.9 Does CGI create new security risks?
  422.  
  423.  
  424. Yes.   Period.
  425. There is a lot you can do to minimise these.   The most important thing
  426. to do is read and understand Lincoln Stein's excellent WWW security
  427. FAQ, at http://www.w3.org/Security/Faq/www-security-faq.html
  428.  
  429.  
  430. ------------------------------
  431.  
  432. Subject: 1.10 Do I need to be on Unix?
  433.  
  434.  
  435. No, but it helps.   The Web, along with the Internet itself, C, Perl,
  436. and almost every other Good Thing in the last 20 years of computing,
  437. originated in Unix.   At the time of writing, this is still the
  438. most mature and best-supported platform for Web applications.
  439.  
  440.  
  441. ------------------------------
  442.  
  443. Subject: 1.11 Do I have to use Perl?
  444.  
  445.  
  446. No - you can use any programming language you please.   Perl is simply
  447. today's most popular choice for CGI applications.   Some other widely-
  448. used languages are C, C++, TCL, BASIC and - for simple tasks -
  449. even shell scripts.
  450.  
  451. Reasons for choosing Perl include its powerful text manipulation
  452. capabilities (in particular the 'regular' expression) and the fantastic
  453. WWW support modules available.
  454.  
  455.  
  456. ------------------------------
  457.  
  458. Subject: 1.12 What languages should I know/use?
  459.  
  460.  
  461. It isn't really that important.  Use what you're comfortable with,
  462. or what you're constrained (eg by your manager) to use.
  463.  
  464. If you're just dabbling with programming, Perl is a good choice, simply
  465. because of the wealth of ready-to-run Perl/CGI resources available.
  466.  
  467. If you're serious about programming, you should be at home in a
  468. range of languages.  C, the industry standard, is a must (at least to
  469. the level of comfortably reading other people's code).  You'll
  470. certainly want at least one scripting language such as Perl, Python
  471. or Tcl.  C++ is also a good idea.
  472.  
  473. In response to a Usenet newbie question:
  474. >  I am seriously wanting to learn some CGI programming languages
  475.  
  476. J.M. Ivler wrote some eloquent words of wisdom:
  477. > If you want to learn a programming language, learn a programming language.
  478. > If you want to learn how to do CGI programming, learn a programming
  479. > language first.
  480. > My book is one of the few that tackles two languages at the same time.
  481. > Why? because it's not about languages (which are just syntax for logic).
  482. > CGI programming is about programming, and how to leverage the experience
  483. > for the person coming to the site, or maintaining the site, or in some way
  484. > meeting some requirements. Language is just a tool to do so.
  485.  
  486.  
  487. ------------------------------
  488.  
  489. Subject: 1.13 Do I have to put it in cgi-bin?
  490.  
  491.  
  492. see next question
  493.  
  494.  
  495. ------------------------------
  496.  
  497. Subject: 1.14 Do I have to call it *.cgi?  *.pl?
  498.  
  499.  
  500. Maybe.   It depends on your server installation.
  501.  
  502. These types of filenames are commonly used conventions - no more.
  503. It is up to the server administrator whether or not CGI scripts are
  504. enabled, and (if so) what conventions tell the server to run or
  505. to print them.
  506.  
  507. If you are running your own server, read the manual.
  508. If you're on ISP or other rented webspace, check their webpages for
  509. information or FAQs.   As a last resort, ask the server administrator.
  510.  
  511.  
  512. ------------------------------
  513.  
  514. Subject: 1.15 What is the "CGI Overhead", and should I be worried about it?
  515.  
  516.  
  517. The CGI Overhead is a consequence of HTTP being a stateless protocol.
  518. This means that a CGI process must be initialised for every "hit"
  519. from a browser.
  520.  
  521. In the first instance, this usually means the server forking a
  522. new process.  This in itself is a modest overhead, but it can
  523. become important on a heavily-used server if the number of
  524. processes grows to problem levels.
  525.  
  526. In the second place, the CGI program must initialise.  In the
  527. case of a compiled language such as C or C++ this is negligible,
  528. but there is a small penalty to pay for scripting languages such as Perl.
  529.  
  530. Thirdly, CGI is often used as 'glue' to a backend program, such as
  531. a database, which may take some considerable time to initialise.
  532. This represents a major overhead, which must be avoided in any
  533. serious application.  The most usual solution is for the backend
  534. program to run as a separate server doing most of the work, while
  535. the actual CGI simply carries messages.
  536.  
  537. Fourthly, some CGI scripts are just plain inefficient, and may
  538. take hundreds of times the resources they need.  Programs using
  539. system() or `backtick` notation often fall into this category.
  540.  
  541. Note that there are ways to reduce or eliminate all these overheads,
  542. but these tend to be system- or server-specific.  The best-supported
  543. server is probably Apache, as commercial server-vendors may prefer to
  544. push their proprietary solutions in preference to CGI.
  545.  
  546.  
  547. ------------------------------
  548.  
  549. Subject: 1.16 What do I need to know about file permissions and "chmod"?
  550.  
  551.  
  552. Unix systems are designed for multiple users, and include provision
  553. for protecting your work from unauthorised access by other users
  554. of the system.  The file permissions determine who is permitted
  555. to do what with your programs, data, and directories.  The command
  556. that sets file permissions is chmod.
  557.  
  558. Web servers typically run as user "nobody".  That means that, setting
  559. aside serious bugs (such as those in certain versions of the Frontpage
  560. extensions), your files are absolutely secure from damage through the
  561. webserver.  It also means that you may have to make explicit changes to
  562. enable the server to access them in a CGI context.
  563.  
  564. There are two ways to run CGI:
  565. - by default they run as the webserver user (nobody)
  566.     For most purposes this is safest, as your programs and data
  567.     are protected by the operating system from unauthorised access
  568.     through possible bugs in your CGI.  However, when the CGI has
  569.     to write to a file, that file must be writable to every web
  570.     user on the system, and is therefore completely unprotected.
  571. - setuid, they run under your own userid.
  572.     This means that files written by your CGI can be secure.
  573.     On the other hand, any bugs in your CGI could now compromise
  574.     *all* your programs and data on the server.
  575.     As an elementary security precaution, scripts (e.g. Perl) are
  576.     prevented from running setuid by most OSs.  The "cgiwrap"
  577.     program offers a workaround for this.
  578.  
  579. A third way you should *never* permit CGI to be run is:
  580. - as root or setuid root, they can run as any user.
  581.     This is extremely dangerous, as any bugs could compromise the
  582.     entire server, including every user's files.  Fortunately only
  583.     the system administrator can install setuid root programs.  If
  584.     you are *at all* concerned about security, make sure that no such
  585.     programs (in particular Frontpage extensions) are installed,
  586.     regardless of whether you use them yourself.
  587.  
  588. For a proper overview, "man chmod".  Some modes that may be useful
  589. in a typical CGI context are:
  590.  
  591. * CGI programs, 0755
  592. * data files to be readable by CGI, 0644
  593. * directories for data used by CGI, 0755
  594. * data files to be writable by CGI, 0666 (data has absolutely no security)
  595. * directories for data used by CGI with write access, 0777 (no security)
  596. * CGI programs to run setuid, 4755
  597. * data files for setuid CGI programs, 0600 or 0644
  598. * directories for data used by setuid CGI programs, 0700 or 0755
  599. * For a typical backend server process, 4750
  600.  
  601. Finally, if this answer tells you anything you didn't already know,
  602. don't even think about trying to set up a secure server!
  603.  
  604.  
  605. ------------------------------
  606.  
  607. Subject: 1.17 What is CGIWrap, and how does it affect my program?
  608.  
  609.  
  610. [ quoted from http://www.umr.edu/~cgiwrap/intro.html ]
  611.  
  612. > CGIWrap is a gateway program that allows general users to use CGI scripts
  613. > and HTML forms without compromising the security of the http server.
  614. > Scripts are run with the permissions of the user who owns the script. In
  615. > addition, several security checks are performed on the script, which will not
  616. > be executed if any checks fail. 
  617. > CGIWrap is used via a URL in an HTML document. As distributed, cgiwrap
  618. > is configured to run user scripts which are located in the
  619. > ~/public_html/cgi-bin/ directory. 
  620.  
  621. See http://www.umr.edu/~cgiwrap/
  622.  
  623.  
  624. ------------------------------
  625.  
  626. Subject: 1.18 How do I decode the data in my Form?
  627.  
  628.  
  629. The normal format for data in HTTP requests is URLencoded.   All Form data
  630. is encoded in a string, of the form
  631.     param1=value1¶m2=value2&...paramn=valuen
  632. Many non-alphanumeric characters are "escaped" in the encoding:
  633. the character whose hexadecimal number is "XY" will be represented by
  634. the character string "%XY".
  635.  
  636. Decoding this string is a fundamental function of every CGI library.
  637.  
  638. Another format is "multipart/form-data", also known as "file upload".
  639. You will get this from the HTML markup
  640. <form method="POST" enctype="multipart/form-data">
  641.  
  642. (but note you must accept URLencoded input in any case, since not all
  643. browsers support multipart forms).
  644.  
  645. Most(?) CGI libraries will handle this transparently.
  646.  
  647.  
  648. -------------------------------------------------------------
  649.  
  650. Subject: SECTION 2 -   HTTP HEADERS AND NPH SCRIPTS
  651.  
  652. This is a fairly technical section dealing with HTTP, the protocol of
  653. the Web. It also includes NPH, the mechanism by which CGI programs can
  654. return HTTP header information directly to the Client.
  655.  
  656.  
  657. ------------------------------
  658.  
  659. Subject: 2.1 What is HTTP (HyperText Transfer Protocol)?
  660.  
  661.  
  662. HTTP is the protocol of the Web, by which Servers and Clients (typically
  663. browsers) communicate.  An HTTP transaction comprises a Request sent by
  664. the Client to the Server, and a Response returned from the Server to
  665. the Client.
  666. Every HTTP request and response includes a message header, describing
  667. the message.   These are processed by the HTTPD, and may often be
  668. mostly ignored by CGI applications (but see below).
  669. A message body may also be included:
  670.   1) A HEAD or GET request sends only a header.   Any form data is encoded 
  671.      in an HTTP_QUERY_STRING header field, which is available to the CGI
  672.      program as an environment variable QUERY_STRING.
  673.   2) A POST request sends both header and body.   The body typically
  674.      comprises data entered by a user in a form.
  675.   3) A HEAD request does not expect a body in the response.
  676.   4) A GET or POST request will accept a response with or without a body,
  677.      according to the header.   The body of a response is typically an
  678.      HTML document.
  679.  
  680.  
  681. ------------------------------
  682.  
  683. Subject: 2.2 What HTTP request headers can I use?
  684.  
  685.  
  686. Most HTTP request headers are passed to the CGI script as environment
  687. variables.   Some are guaranteed by the CGI spec.   Others are server,
  688. browser and/or application dependent.
  689.  
  690. To see what _your_ browser and server are telling each other, just use
  691. a trivial little CGI script to print out the environment.   In Unix:
  692.     #!/bin/sh
  693.     echo "Content-type: text/plain"
  694.     echo
  695.     set
  696.  
  697. (Just call it "env.cgi" or something, and put it where your server
  698. will execute it.   Then point your browser at
  699. http://your.server/path/to/env.cgi ).
  700.  
  701. This enables you to see at-a-glance what useful server variables are set.
  702. Note that dumping the environment like this within a more complex
  703. script can be a useful debugging technique.
  704.  
  705. For details, see the CGI Environment Variables specification at
  706. http://hoohoo.ncsa.uiuc.edu/cgi/env.html
  707. (which also includes a version of the above script - somewhat more
  708. nicely formatted - online).
  709.  
  710.  
  711. ------------------------------
  712.  
  713. Subject: 2.3 What Environment variables are available to my application?
  714.  
  715.  
  716. See previous question.   Those you can rely on are documented in NCSA's
  717. pages; those associated with your particular server and browser can
  718. be determined using the above script.
  719.  
  720.  
  721. ------------------------------
  722.  
  723. Subject: 2.4 Why doesn't my script get REMOTE_USER?  My page is password-protected.
  724.  
  725.  
  726. You will get REMOTE_USER if the _script_ is password protected.
  727. That's all.  The page the user is coming from has nothing to do with it.
  728.  
  729.  
  730. ------------------------------
  731.  
  732. Subject: 2.5 What HTTP response headers do I need to know about?
  733.  
  734.  
  735. Unless you are using NPH, the HTTPD will insert necessary response
  736. headers on your behalf, always provided it is configured to do so.
  737.  
  738. However, it is conventional for servers to insert the Content-Type header
  739. based on a page's filename, and CGI scripts cannot rely on this.  Hence
  740. the usual advice is to print an explicit Content-Type header.
  741. At least one of "Content-Type", "Status" and "Location" is almost
  742. always required.
  743.  
  744. A few other headers you may wish to use explicitly are:
  745. Status        (to set HTTP return code explicitly.   Caveats:
  746.            (1) Behaviour is undefined if it conflicts with
  747.            another header. (2) This is NOT an HTTP header.)
  748. Location    (to redirect the user to another URI, which may or may
  749.         not be on your own server)
  750. Set-cookie    (Netscape/Nonstandard) Set a cookie
  751. Refresh        (Netscape/Nonstandard) Clientpull
  752.  
  753. You can also use general MIME headers: eg "Keywords" for the benefit of
  754. indexers (although in this instance some major search robots have
  755. regrettably introduced a new protocol to do the same thing).
  756.  
  757. For a detailed reference, see RFC1945 (HTTP/1.0) or RFC2068 (HTTP/1.1).
  758.  
  759.  
  760. ------------------------------
  761.  
  762. Subject: 2.6 What is NPH?
  763.  
  764.  
  765. NPH = No Parsed Headers.   The script undertakes to print the entire
  766. HTTP response including all necessary header fields.   The HTTPD
  767. is thereby instructed not to parse the headers (as it would normally do)
  768. nor add any which are missing.
  769.  
  770.  
  771. ------------------------------
  772.  
  773. Subject: 2.7 Must/should/can I write nph scripts?
  774.  
  775.  
  776. Generally, no.   It is usually better to save yourself hassle by letting
  777. the HTTPD produce the headers for you.
  778.  
  779. If you are going to use NPH, be sure to read and understand the HTTP spec at
  780. http://www.w3.org/pub/WWW/Protocols/
  781.  
  782. Your headers should be complete and accurate, because you're instructing
  783. the HTTPD not to correct them or insert what's missing.
  784.  
  785. Possible circumstances where the use of NPH is appropriate are:
  786.   * When your headers are sufficiently unusal that they might be
  787.     differently parsed by different HTTPDs (eg combining "Location:"
  788.     with a "Status:" other than 302).
  789.   * When returning output over a period of time (eg displaying
  790.     unbuffered results of a slow operation in 'real' time).
  791. See RFC1945 (HTTP/1.0) or RFC2068 (HTTP/1.1) for detail
  792.  
  793.  
  794. ------------------------------
  795.  
  796. Subject: 2.8 Do I have to call it nph-*
  797.  
  798.  
  799. According to NCSA's reference pages, this is the standard for telling
  800. the server that your script is NPH, so this should be a fully portable
  801. convention.
  802.  
  803.  
  804. ------------------------------
  805.  
  806. Subject: 2.9 What is the difference between GET and POST?
  807.  
  808.  
  809. Firstly, the the HTTP protocol specifies differing usages for the two
  810. methods.   GET requests should always be idempotent on the server.
  811. This means that whereas one GET request might (rarely) change some state
  812. on the Server, two or more identical requests will have no further effect.
  813.  
  814. This is a theoretical point which is also good advice in practice.
  815. If a user hits "reload" on his/her browser, an identical request will be
  816. sent to the server, potentially resulting in two identical database or
  817. guestbook entries, counter increments, etc.   Browsers may reload a
  818. GET URL automatically, particularly if cacheing is disabled (as is usually
  819. the case with CGI output), but will typically prompt the user before
  820. re-submitting a POST request.   This means you're far less likely to get
  821. inadvertently-repeated entries from POST.
  822.  
  823. GET is (in theory) the preferred method for idempotent operations, such
  824. as querying a database, though it matters little if you're using a form.
  825. There is a further practical constraint that many systems have builtin
  826. limits to the length of a GET request they can handle: when the total size
  827. of a request (URL+params) approaches or exceeds 1Kb, you are well-advised
  828. to use POST in any case.
  829.  
  830. In terms of mechanics, they differ in how parameters are passed to the
  831. CGI script.   In the case of a POST request, form data is passed on
  832. STDIN, so the script should read from there (the number of bytes to be
  833. read is given by the Content-length header).   In the case of GET, the
  834. data is passed in the environment variable QUERY_STRING.   The content-type
  835. (application/x-www-form-urlencoded) is identical for GET and POST requests.
  836.  
  837.  
  838. -------------------------------------------------------------
  839.  
  840. Subject: SECTION 3 -   TECHNIQUES: "HOW DO I..."
  841.  
  842. This section comprises programming hints and tips for a number of popular
  843. tasks. Also included are a number of common questions to which the answer
  844. is "you can't", with the reasons why.
  845.  
  846.  
  847. ------------------------------
  848.  
  849. Subject: 3.1 Can I get information about who is visiting?
  850.  
  851.  
  852. *sigh*
  853. Many people keep mailing me questions or suggested hacks to get
  854. visitor information, particularly email addresses.   It seems they
  855. won't take "NO" for an answer.
  856.  
  857. The bottom line is that whatever information is available to _you_
  858. is _equally_ available to every spammer on the net.   Therefore when
  859. a browser bug _does_ permit personal data to be collected, it gets
  860. reported and fixed very quickly (one short-lived Netscape 2.0.x
  861. release reportedly had such a bug in its Javascript engine).
  862.  
  863. You can get some limited information from the environment variables
  864. passed to you by the browser.   Relatively few of these are guaranteed
  865. to be available, and some may be misleading.   For particular types
  866. of information, see below.   For full details, see NCSA's reference pages.
  867.  
  868.  
  869. ------------------------------
  870.  
  871. Subject: 3.2 Can I get the email of visitors?
  872.  
  873.  
  874. Why do you want to do this?
  875.  
  876. The best information available is the REMOTE_ADDR and REMOTE_HOST,
  877. which tell you nothing about the user.   Techniques such as "finger@"
  878. are not reliable, are widely disliked, and generally serve only to
  879. introduce long delays in your CGI.   Better - as well as more polite -
  880. just to ask your users to fill in a form.
  881.  
  882. BTW: the "From:" header line (HTTP_FROM variable) is usually only set
  883. by robots, since human visitors to your webpage will not normally want
  884. their addresses collected without permission, and browsers respect this.
  885.  
  886.  
  887. ------------------------------
  888.  
  889. Subject: 3.3     "But I saw some.kool.site display my email address..."
  890.  
  891.  
  892. Some sites will play party tricks, which can get *some users* email
  893. addresses.   Possible tell-tale signs of this are inordinate delays
  894. loading a page (fingering @REMOTE_HOST - doesn't often work but
  895. probably can't be detected from the webpage), or a submit button that
  896. appears to do nothing at all (a mailto: form - works well with some
  897. browsers but trivially detectable).   As a "snoop" party trick that's
  898. fine, but if you find someone abusing these facilities (eg they send
  899. you junkmail), alert their service provider!
  900.  
  901.  
  902. ------------------------------
  903.  
  904. Subject: 3.4 Can I verify the email addresses people enter in my Form?
  905.  
  906.  
  907. Unfortunately people will sometimes enter an incorrect or invalid
  908. email address in your Form.   Worse, they may enter a valid but
  909. incorrect email address that will deliver to someone who doesn't
  910. want your mail.
  911.  
  912. Proposed regexps to match email addresses are sometimes posted.
  913. Most of these will fail against perfectly valid email addresses,
  914. like "S=N.OTHER/OU1=X12345A/RECIPNUM=1/MTA-BASIC@attmail.com"
  915. (which is what your address looks like if you are connected to
  916. the Internet via X400 - and if you think that example is too easy,
  917. check the ones at the end of Eli the Bearded's Email Addressing FAQ).
  918.  
  919. Probably the most complete parser and checker available for download
  920. is Tom Christiansen's, at
  921. http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz
  922. Of course, this still says nothing about deliverability.
  923.  
  924. A frequently-suggested hack that doesn't work is to use
  925. SMTP EXPN or VRFY commands.   Modern versions of sendmail permit
  926. administrators to disable these commands, and many sites take
  927. advantage of this facility to protect their users' privacy.
  928.  
  929. Probably the best way to verify an email address is to send mail to
  930. it, asking the user to respond.   Include a clause like "if you have
  931. received this mail in error, please accept our apologies..."
  932.  
  933.  
  934. ------------------------------
  935.  
  936. Subject: 3.5 Subject: How can I get the hostname of the remote user?
  937.  
  938.  
  939. You can't. Well, not always.
  940.  
  941. IF it is available, you'll find it in the REMOTE_HOST environment
  942. variable.  However, this will more often than not contain the numerical
  943. IP address rather than the IP name of the remote host. Remember that
  944. not all IP addresses have a hostname associated with them; this is the
  945. case of most IP addresses assigned to dialup users, for example. Your
  946. web server may also not perform a reverse lookup on incoming
  947. connections, in which case REMOTE_HOST will contain the IP address even
  948. if it has a corresponding IP name. In the second case, you can do a
  949. reverse lookup yourself in your script, but this is expensive and
  950. should probably be avoided unless absolutely necessary.
  951.  
  952. Even if you do manage to obtain a hostname, you should be aware that it
  953. may not correspond to the hostname the user is accessing your page
  954. from. It may instead be that of an intervening proxy host.
  955.  
  956. The short answer is therefore that there is no reliable way of finding
  957. out what the remote user's hostname is.
  958.  
  959.  
  960. ------------------------------
  961.  
  962. Subject: 3.6 Can I get browser details and return different pages?
  963.  
  964.  
  965. Why do you want to do this?
  966.  
  967. Well-written HTML will display correctly in any browser, so the correct
  968. answer to this question is to design a template for your output in good
  969. HTML, and make sure your output is correct.
  970.  
  971. If you insist on a different answer, you can use the HTTP_USER_AGENT
  972. environment variable.  This requires care, and can lead to unexpected
  973. results.   For example, checking for "Mozilla" and serving a frameset
  974. to it ensures that you *also* serve the frameset to early (Non-Frame)
  975. Netscapes, me-too browsers (notably Microsoft[1]) and others who have
  976. chosen to lie to you about their browser.
  977.  
  978. Note also that not every User Agent is a browser.   Your page may be
  979. read by a user agent you've never heard of, and then displayed by
  980. 100 different browsers.   Or retrieved by different browsers from
  981. a cache.   Another reason to write good HTML, and not try to
  982. devise a clever or koool substitute.
  983.  
  984. [1] At the time of writing, only Netscape 2+ supported frames, and
  985.     some authors considered them koool.  That's changed, but the same
  986.     general principle still holds.
  987.  
  988.  
  989. ------------------------------
  990.  
  991. Subject: 3.7 Can I trace where a user has come from/is going to?
  992.  
  993.  
  994. HTTP_REFERER might or might not tell you anything.   By all means
  995. use it to collect partial statistics if you participate in (say)
  996. an advertising banner scheme.   But it is not always set, and may
  997. be meaningless (eg if a user has accessed your page from a bookmark,
  998. and the browser is too dumb to cope with this).
  999.  
  1000. The HTTP protocol forbids relying on Referer information for functionality
  1001. in your programs, so don't try it.
  1002.  
  1003. You cannot trace outgoing links at all.   If you really must try,
  1004. point all the external links to your HTTPD and use its redirection
  1005. facility (which gives you generally-reliable logs).   This is much
  1006. less inefficient than using a CGI script.
  1007.  
  1008. BTW: don't even think about asking Javascript to send you information
  1009. on some event: it's a violation of privacy which Netscape fixed as
  1010. soon as complaints about its abuse started coming in.   If it works
  1011. with *your* browser, you should upgrade!
  1012.  
  1013.  
  1014. ------------------------------
  1015.  
  1016. Subject: 3.8 Can I launch a long process and return a page before it's finished?
  1017.  
  1018.  
  1019. [UNIX]
  1020. You have to fork/spawn the long-running process.
  1021. The important thing to remember is to close all its file descriptors;
  1022. otherwise nothing will be returned to the browser until it's finished.
  1023. The standard trick to accomplish this is redirection to/from /dev/null:
  1024.  
  1025.         "long_process < /dev/null > /dev/null 2>&1 &"
  1026.         print HTML page as usual
  1027.  
  1028.  
  1029. ------------------------------
  1030.  
  1031. Subject: 3.9 Can I launch a long process which the user interacts with?
  1032.  
  1033.  
  1034. This does not fit well with the basic mechanics of the Web, in which
  1035. each transaction comprises a single request and response.
  1036. If your processing can be done on the Client machine, you can use
  1037. a clientside application; for example a Java applet.
  1038.  
  1039. For processing on the server, one trick that works well for Clients
  1040. running an X server (and far more efficient than a JAVA solution) is:
  1041.   if ( fork() ) {
  1042.     print HTML page explaining what's going on and advising about xhost
  1043.   } else {
  1044.     exec ("xterm -display THEIR_DISPLAY -title MY_APP -e MY_PROG ARGS
  1045.         < /dev/null > /dev/null 2>&1 &") ;
  1046.   }
  1047. NOTE: THEIR_DISPLAY is not necessarily the same as REMOTE_HOST or REMOTE_ADDR.
  1048. You have to ask users to supply their display (set REMOTE_HOST as default).
  1049.  
  1050. A JAVA terminal program will accomplish something similar for the many
  1051. users with platforms that support JAVA but not X.
  1052.  
  1053.  
  1054. ------------------------------
  1055.  
  1056. Subject: 3.10 Can I password-protect my pages?
  1057.  
  1058.  
  1059. Yes.   Use your HTTPD's authentication, just as you would a basic HTML page.
  1060. Now you'll have the identity of every visitor in REMOTE_USER.
  1061.  
  1062.  
  1063. ------------------------------
  1064.  
  1065. Subject: 3.11 Can I do HTTP authentication using CGI?
  1066.  
  1067.  
  1068. It depends on which version of the question you asked.
  1069.  
  1070. Yes, you can use CGI to trigger the browser's standard Username/Password
  1071. dialogue.   Send a response code 401, together with a "WWW-authenticate"
  1072. header including details of the the authentication scheme and realm:
  1073. e.g. (in a non-NPH script)
  1074.  
  1075.     Status: 401 Unauthorized to access the document
  1076.     WWW-authenticate: Basic realm="foobar"
  1077.     Content-type: text/plain
  1078.  
  1079.     Unauthorised to access this document
  1080.  
  1081. The use you can make of this is server-dependent, and harder,
  1082. since most servers expect to deal with authentication before ever
  1083. reaching the CGI (eg through .www_acl or .htaccess).
  1084. Thus it cannot usefully replace the standard login sequence, although
  1085. it can be applied to other situations, such as re-validating a user -
  1086. e.g after a certain timeout period or if the same person may need to
  1087. login under more than one userid.
  1088.  
  1089. What you can never get in CGI is the credentials returned by the user.
  1090. The HTTPD takes care of this, and simply sets REMOTE_USER to the
  1091. username if the correct password was entered.
  1092.  
  1093. For a much longer but outdated discussion of this question,
  1094. see my discussion at http://www.webthing.com/tutorials/login.html
  1095.  
  1096.  
  1097. ------------------------------
  1098.  
  1099. Subject: 3.12 Can I identify users/sessions without password protection?
  1100.  
  1101.  
  1102. The most usual (but browser-dependent) way to do this is to set a cookie.
  1103. If you do this, you are accepting that not all users will have a 'session'.
  1104.  
  1105. An alternative is to pass a session ID in every GET URL, and in hidden
  1106. fields of POST requests.   This can be a big overhead unless _every_ page
  1107. requires CGI in any case.
  1108.  
  1109. Another alternative is the Hyper-G[1] solution of encoding a session-id in
  1110. the URLs of pages returned:
  1111.     http://hyper-g.server/session_id/real/path/to/page
  1112. This has the drawback of making the URLs very confusing, and causes any
  1113. bookmarked pages to generate old session_ids.
  1114.  
  1115. Note that a session ID based solely on REMOTE_HOST (or REMOTE_ADDR)
  1116. will NOT work, as multiple users may access your pages concurrently
  1117. from the same machine.
  1118.  
  1119. [1] Actually I don't think that's been true of Hyper-G since sometime
  1120. in '96.  However, general advances in web server technology, such as
  1121. Apache's mod_alias or mod_rewrite, make it straightforward without
  1122. the need for CGI.
  1123.  
  1124.  
  1125. ------------------------------
  1126.  
  1127. Subject: 3.13 Can I redirect users to another page?
  1128.  
  1129.  
  1130. For permanent and simple redirection, use the HTTPD configuration file:
  1131. it's much more efficient than doing it yourself.   Some servers enable
  1132. you to do this using a file in your own directory (eg Apache) whereas
  1133. others use a single configuration file (eg CERN).
  1134.  
  1135. For more complicated cases (eg process form inputs and conditionally
  1136. redirect the user), use the "Location:" response header.
  1137. If the redirection is itself a CGI script,  it is easy to URLencode
  1138. parameters to it in a GET request, but don't forget to escape the URL!
  1139.  
  1140.  
  1141. ------------------------------
  1142.  
  1143. Subject: 3.14 Can I run a CGI script without returning a new page to the browser?
  1144.  
  1145.  
  1146. Yes, but think carefully first:  How are your readers going to know
  1147. that their "submit" has succeeded?   They may hit 'submit' many times!
  1148.  
  1149. The correct solution according to the HTTP specification is to
  1150. return HTTP status code 204.   As an NPH script, this would be:
  1151.  
  1152.     #!/bin/sh
  1153.     # do processing (or launch it as background job)
  1154.     echo "HTTP/1.0 204 No Change"
  1155.     echo
  1156.  
  1157. (as non-NPH, you'd simply replace HTTP/1.0 with the Status: CGI header).
  1158.  
  1159. Alan J Flavell has pointed out that this will fail with certain
  1160. popular browsers, and suggests a workaround to accommodate them:
  1161.  
  1162. [ May 1998 update[1]: I'm deleting Alan's suggestion, because the problem
  1163.   is mainly of historical interest, and the workaround is no longer
  1164.   recommended.  See his page for a a detailed survey and recommendations.
  1165. ]
  1166.  
  1167. His survey is at
  1168. http://ppewww.ph.gla.ac.uk/%7Eflavell/status204/results.html
  1169.  
  1170. [1] With apologies to Alan for having left it in so long.
  1171.  
  1172.  
  1173. ------------------------------
  1174.  
  1175. Subject: 3.15 Can I write output to a different Netscape frame?
  1176.  
  1177.  
  1178. Yep.   The fact you're using CGI makes no difference: use
  1179. "target=" in your links as usual.   Alternatively, the script
  1180. can print a "Window-target:" header.   Read Netscape's pages
  1181. for detail: these answer all the questions about things like
  1182. "getting rid of" or "breaking out of" frames, too.
  1183.  
  1184.  
  1185. ------------------------------
  1186.  
  1187. Subject: 3.16 Can I write output to several frames at once?
  1188.  
  1189.  
  1190. A single CGI script can only ever print to one frame.
  1191.  
  1192. However, this limitation may be overcome by using more than one script.
  1193. The first script (the URL of the "submit" button) prints a frameset,
  1194. typically to a "_parent" or "_top" target.   The sources for one or
  1195. more of the frames thus generated may also be CGI scripts, to which
  1196. you can easily pass parameters (eg encoded in URLs with method GET).
  1197. This hack is definitely not recommended.   If you find yourself wanting
  1198. to update several frames from a single user event, it probably means
  1199. you should review the design of your application at a higher level.
  1200.  
  1201. Warnings:
  1202.  1. Don't forget to escape your URLs.
  1203.  2. This technique results in your server being hit by multiple 
  1204.     concurrent CGI requests.   You'll need LOTS of memory, especially
  1205.     if you use a memory-hog like Perl.   It can be a good recipe
  1206.     for bringing a server to its knees.
  1207.  
  1208. Javascript is often a valid alternative here, but note just how silly
  1209. it can (and often does) look in a different browser.
  1210.  
  1211.  
  1212. ------------------------------
  1213.  
  1214. Subject: 3.17 Can I use a CGI script to generate both text and inline images?
  1215.  
  1216.  
  1217. Not directly.   One script generates one response to one request.
  1218.  
  1219. If you want to generate a dynamic page including dynamic images
  1220. (say, a report including graphs, all of which depend on user input)
  1221. then your primary script will print the usual
  1222.    <img src="[script-to-generate-image]" alt="[what you asked for]">
  1223. and, just as in the multiple frames case, you can pass data to the
  1224. image-generating program encoded in a GET URL.   Of course, the same
  1225. caveats apply: see above.
  1226.  
  1227.  
  1228. ------------------------------
  1229.  
  1230. Subject: 3.18 How can I use Caches to make CGI scripts faster and more Net-friendly?
  1231.  
  1232.  
  1233. This is currently beyond the scope of this FAQ.   However,
  1234. there is an excellent introduction to net-friendly webpages, including
  1235. CGI pages, at http://vancouver-webpages.com/CacheNow/
  1236.  
  1237. A sample cacheing perl/cgi script by Andrew Daviel is available at
  1238. http://vancouver-webpages.com/proxy/log-tail.pl
  1239.  
  1240.  
  1241. ------------------------------
  1242.  
  1243. Subject: 3.19 How can I avoid users hitting "submit" twice?
  1244.  
  1245.  
  1246. You can't.   You just have to deal with it when they do.
  1247.  
  1248. You can avoid re-processing a submission by embedding a unique ID in your
  1249. Form each time it is displayed.   When you process the form, you enter
  1250. the ID in a database.  Or, if it's already there, you don't repeat the
  1251. processing.
  1252.  
  1253. You probably want to expire your database entries after a little time:
  1254. an hour should be fine in a typical situation.
  1255.  
  1256. If you're already using cookies (e.g. a shoppingcart), an alternative is
  1257. to use the cookie as a unique identifier.   This means you also have to
  1258. handle the situation where a user deliberately "goes round twice" and
  1259. submits the same form with different contents.
  1260.  
  1261. If your script may take some time to process, you should also consider
  1262. running it as a background job, and returning an immediate
  1263. acknowledgement to the user (see above if your "immediate" response
  1264. gets delayed until processing is complete in any case).
  1265.  
  1266.  
  1267. ------------------------------
  1268.  
  1269. Subject: 3.20 How can I stop my CGI script reading and writing files as "nobody"?
  1270.  
  1271.  
  1272. CGI scripts are run by the HTTPD, and therefore by the UID of the HTTPD
  1273. process, which is (by convention) usually a special user "nobody".
  1274.  
  1275. There are two basic ways to run a script under your own userid:
  1276. (1) The direct approach: use a setuid program.
  1277. (2) The double-server approach: have your CGI script communicate
  1278.     with a second process (e.g. a daemon) running under your userid,
  1279.     which is responsible for the actual file management.
  1280.  
  1281. The direct approach is usually faster, but the client-server architecture
  1282. may help with other problems, such as maintaining integrity of a database.
  1283.  
  1284. When running a compiled CGI program (e.g. C, C++), you can make it
  1285. setuid by simply setting the setuid bit:
  1286. e.g. "chmod 4755 myprog.cgi"
  1287.  
  1288. For security reasons, this is not possible with scripting languages
  1289. (eg Perl, Tcl, shell).   A workaround is to run them from a setuid
  1290. program, such as cgiwrap.
  1291.  
  1292. In most cases where you'd want to use the client-server approach,
  1293. the server is a finished product (such as an SQL server) with its
  1294. own CGI interface.
  1295. A lightweight alternative to this is Don Libes' "expect" package.
  1296.  
  1297. Note that any program running under your userid has access to all your
  1298. files, and could do serious damage if hacked.   Take care!
  1299.  
  1300.  
  1301. ------------------------------
  1302.  
  1303. Subject: 3.21 How can I prevent my CGI results being cached by the browser?
  1304.  
  1305.  
  1306. Firstly, we need to debunk a myth.  People asking this question usually
  1307. add that they tried "Pragma: no-cache".  Whilst this is not actively
  1308. wrong, there is no requirement on browsers to take any notice of it,
  1309. and most of them don't.
  1310.  
  1311. The "Pragma: no-cache" header (now superseded by HTTP/1.1 Cache-Control)
  1312. is a directive to proxies.  The browser sends it with an HTTP request
  1313. to indicate that it wants the request to be dealt with by the original
  1314. server and will not accept a proxy's cached document (e.g. when you
  1315. use a reload button).  The server may send it to tell a proxy not to
  1316. cache the document.
  1317.  
  1318. Having said all that, a practical hack to get round cacheing is
  1319. to use a different URL for your CGI script each time it's called.
  1320. This can easily be accomplished by adding a unique identifier such
  1321. as current time in the QUERY_STRING or PATH_INFO.  The browser will
  1322. see a different URL, but the script can just ignore it.  Note that
  1323. this can be very inefficient, and should be avoided where possible.
  1324.  
  1325.  
  1326. ------------------------------
  1327.  
  1328. Subject: 3.22 How can I control the default filename when downloading a file via CGI?
  1329.  
  1330.  
  1331.     (from a newsgroup post by Matthew Healy)
  1332.  
  1333. One option, assuming you aren't already using the PATH_INFO
  1334. environment variable, is just to call your CGI script with extra
  1335. path information.
  1336.  
  1337. For example, suppose the URL to your script is actually
  1338.  
  1339. http://server.com/scriptname?name1=value1&name2=value2
  1340.  
  1341. Instead, try calling it as
  1342.  
  1343. http://server.com/scriptname/filename.ext?name1=value1&name2=value2
  1344.  
  1345. and note that you need to escape the URL if it's in an HTML page:
  1346.  
  1347. http://server.com/scriptname/filename.ext?name1=value1&name2=value2
  1348.  
  1349. And probably the browser will assign the name given in the last chunk
  1350. as the suggested filename for downloading.
  1351.  
  1352. This works because the http server looks for the program file to run,
  1353. then passes any extra path to the program as PATH_INFO variable; the
  1354. browser cannot tell where the SCRIPT_NAME part ends and the PATH_INFO
  1355. part begins.
  1356.  
  1357. This can also be very useful if you want one script to generate more
  1358. than one filename -- the script can check the PATH_INFO value and
  1359. alter its response accordingly...
  1360.  
  1361.  
  1362. -------------------------------------------------------------
  1363.  
  1364. Subject: SECTION 4 -   TROUBLESHOOTING A CGI APPLICATION
  1365.  
  1366. Since this subject is quite well covered by other documents, this FAQ has
  1367. relatively little to say. 
  1368.  
  1369. Eric Wienke has a page "Debugging CGI Scripts 101" at
  1370. http://www.liquidsilver.com/scripts/debug101.html
  1371.  
  1372. Tom Christiansen's "Idiot's guide to solving Perl/CGI problems" is a
  1373. slightly tongue-in-cheek list of common problems, and how to track
  1374. them down.  Much of what Tom covers is not specifically Perl, but
  1375. applies equally to CGI programming in other languages. 
  1376.  
  1377. Marc Hedlund's CGI FAQ and Thomas Boutell's WWW FAQ also
  1378. deal with this subject. 
  1379.  
  1380. See "Further Reading" below (if you don't already know where to find these
  1381. documents). 
  1382.  
  1383.  
  1384. ------------------------------
  1385.  
  1386. Subject: 4.1 Are there some interactive debugging tools and services available?
  1387.  
  1388.  
  1389. (1) Several CGI programming libraries offer powerful interactive
  1390.     debugging facilities.   These include:
  1391.  
  1392.     - for Perl, Lincoln Stein's CGI.pm
  1393.     (now part of the standard Perl distribution)
  1394.  
  1395.     - for Tcl, Don Libes' cgi.tcl
  1396.     http://expect.nist.gov/cgi.tcl
  1397.  
  1398.     - for C++, Nick Kew's CGI++
  1399.     http://www.webthing.com/cgiplusplus/
  1400.  
  1401. (2) Nathan Neulinger's cgiwrap is another package with debugging aids.
  1402. http://www.umr.edu/~cgiwrap/
  1403.  
  1404. (3) The "mod_cgi" Apache module (new with Apache 1.2) enables you to
  1405. capture script output and errors for diagnosis.
  1406.  
  1407. See also the next question.
  1408.  
  1409.  
  1410. ------------------------------
  1411.  
  1412. Subject: 4.2 I'm having trouble with my headers.   What can I do?
  1413.  
  1414.  
  1415. For simple cases, examining your response headers "by hand" may suffice:
  1416. (1) telnet to the host and port where the server is running - e.g.
  1417.         telnet www.myhost.com 80
  1418. (2) Enter HTTP request.   The most useful for this purpose is usually HEAD; eg
  1419.         HEAD /index.html HTTP/1.0
  1420.         (optional HTTP headers)
  1421.         (followed by a blank line)
  1422. Now you'll get a full HTTP response header back.
  1423.  
  1424. For complex cases, such as sending a request with headers (as a browser
  1425. does) or POSTing a form, this author's free online diagnosis cg-eye is
  1426. included in the respective toolkits at
  1427.      http://www.htmlhelp.org/tools/
  1428.      http://www.webthing.com/valet/
  1429.     This combines an offline cgi "linter" with two online services:
  1430.     (a) Interactive mode permits you to formulate an HTTP request,
  1431.     which is then sent to your server.
  1432.     (b) Live mode submits your form, exactly as it gets it from your
  1433.     browser.
  1434.     In both cases, it will print a detailed report of the transaction,
  1435.     and optionally (if the CGI is producing an HTML page) validate it.
  1436.  
  1437.  
  1438. ------------------------------
  1439.  
  1440. Subject: 4.3 Why do I get Error 500 ("the script misbehaved", or "Internal Server Error")
  1441.  
  1442.  
  1443. Your script must follow the CGI interface, which requires it to print:
  1444. (1) One or more Header lines.
  1445. (2) A blank line
  1446. (3) (optional, but strongly advised) a document body.
  1447.  
  1448. This error means it didn't.
  1449.  
  1450. The Header lines can include anything that's valid under HTTP, but must
  1451. normally include at least one of the three special CGI headers:
  1452.     Content-Type
  1453.     Location
  1454.     Status
  1455.  
  1456. Example (a very minimal HTML page via CGI)
  1457. Content-Type: text/html            <= Header
  1458.                     <= Blank Line
  1459. <title>HelloWorld</title>Hello World    <= Document Body
  1460.  
  1461. A common reason for a script to fail is that it crashed before printing
  1462. the header and blank line (or while these are buffered).  Or that it
  1463. didn't run at all: you _did_ try it from the commandline as well as
  1464. check the file permissions and server configuration, didn't you?
  1465.  
  1466. Another possible reason is that it printed something else - like an
  1467. error message - in the Headers.   Check error logs, put a dummy header
  1468. right at the top (for debugging only), check the "Idiot's Guide",
  1469. and use the debug mode of your CGI library.
  1470.  
  1471.  
  1472. ------------------------------
  1473.  
  1474. Subject: 4.4 I tried to use (Content-Type|Location|whatever), but it appears in my Browser?
  1475.  
  1476.  
  1477. That means you put the line in the wrong place.  It must appear in the
  1478. CGI Header, not the document body.  See previous question.
  1479.  
  1480. It's also possible that you didn't print a header at all, or had a blank
  1481. line or other noise before or in the header, but that the HTTPD has
  1482. corrected this error for you (servers which correct your errors may give
  1483. rise to the "works on A not on B" phenomenon).   See previous question.
  1484.  
  1485.  
  1486. ------------------------------
  1487.  
  1488. Subject: 4.5 How can I run my CGI program 'live' in a debugger?
  1489.  
  1490.  
  1491. David S. Jackson offers the following tip:
  1492.  
  1493. > I have a very good trick for debugging CGIs written in C/C++ running on
  1494. > UNIX. You might want to add it to the debugging section of your CGI faq.
  1495. > First, in your CGI code, at it's start, add "sleep(30);". This will cause
  1496. > the CGI to do nothing for thiry seconds (you may need to adjust this
  1497. > time). Compile the CGI with debuging info ("-g" in gcc) and install the
  1498. > CGI as normal. Next, using your web browser, activate the CGI. It will of
  1499. > course just sit there doing nothing. While it is 'sleeping', find it's PID
  1500. > (ps -a | grep <cgi name>). Load your debugger and attach to that PID
  1501. > ("attach <pid>" in gdb). You will also need to tell it where to find the
  1502. > symbol definitions ("symbol-file <cgi>" in gdb). Then set a break point
  1503. > after the invocation of the sleep function and you are ready to debug. Do
  1504. > be aware that your browser will eventually timeout if it doesn't recieve
  1505. > anything.
  1506.  
  1507. (Anyone know similar tricks for scripting languages)?
  1508.  
  1509.  
  1510. ------------------------------
  1511.  
  1512. Subject: 4.6 I'm using CGI with QUERY_STRING embedded in my HTML, but it gets corrupted?
  1513.  
  1514.  
  1515. The problem is the & character, which has two separate special meanings:
  1516. - In HTTP (and hence CGI) it is a separator in your QUERY_STRING
  1517. - In HTML it is an escape character
  1518.  
  1519. So when it appears in an HTML context, it should be encoded.  If you need
  1520. a link to myprog.cgi with QUERY_STRING "a=1&b=2" you should write
  1521. <a href="myprog.cgi?a=1&b=2">my program</a>
  1522. which the browser's HTML parser will convert to what you wanted.
  1523.  
  1524. There are possible browser problems here, although they appear to be
  1525. limited to older browsers.  Some other approaches are:
  1526. - Use a different separator character in CGI programs when called in this
  1527.   manner.  Or even a completely different encoding.  This is safe, but may
  1528.   be much more work unless your CGI library supports setting a different
  1529.   separator character.
  1530. - Avoid any parameters whose names include that of any HTML entity.
  1531.   This runs a possible risk if the set of entities changes in future,
  1532.   or when browsers introduce proprietary 'extensions'.
  1533.  
  1534.  
  1535. -------------------------------------------------------------
  1536.  
  1537. Subject: SECTION 5 -   FURTHER READING
  1538.  
  1539.  
  1540. ------------------------------
  1541.  
  1542. Subject: 5.1 Other FAQs/collections
  1543.  
  1544.  
  1545. ****    Lincoln Stein's FAQ is probably the most    ****
  1546. ****    important WWW document you will ever read.    ****
  1547.  
  1548. Web Authoring FAQs
  1549. http://www.htmlhelp.org/faq/wdgfaq.htm
  1550.  
  1551. For general WWW issues, the World Wide Web FAQ by Thomas Boutell
  1552. http://www.boutell.com/faq/
  1553.  
  1554. Perl/CGI programming FAQ, by Shishir Gundavaram and Tom Christiansen
  1555. http://www.perl.com/perl/faq/perl-cgi-faq.html
  1556.  
  1557. The Idiot's Guide to solving Perl/CGI problems by Tom Christiansen
  1558. http://www.perl.com/perl/faq/idiots-guide.html
  1559.  
  1560. The WWW Security FAQ by Lincoln Stein
  1561. http://www.w3.org/Security/Faq/www-security-faq.html
  1562.  
  1563. CGI Resources Library
  1564. http://www.cgi-resources.com/
  1565.  
  1566. The WWW Virtual Library
  1567. http://WWW.Stars.com/Vlib/
  1568.  
  1569.  
  1570. ------------------------------
  1571.  
  1572. Subject: 5.2 Reference Pages
  1573.  
  1574.  
  1575. CGI Internet Draft - the official spec
  1576. http://www.golux.com/coar/cgi/
  1577.  
  1578. The Common Gateway Interface (CGI)    - old de facto spec
  1579. http://hoohoo.ncsa.uiuc.edu/cgi/interface.html
  1580.  
  1581. HyperText Transfer Protocol (HTTP)
  1582. http://www.w3.org/pub/WWW/Protocols/HTTP/
  1583.  
  1584. HyperText Markup Language (HTML)
  1585. http://www.w3.org/pub/WWW/MarkUp/
  1586.  
  1587.  
  1588.  
  1589. ------------------------------
  1590.  
  1591. Subject: INDEX
  1592.  
  1593. The index is generated from an arbitrary list of keywords.
  1594. If I've missed anything obvious that should be here, please let me know.
  1595.  
  1596.  
  1597. APACHE            1.15, 3.12, 3.13, 4.1
  1598. ASP               1.5, 1.7
  1599. AUTHENTICATION    3.10, 3.11
  1600. BACKGROUND        3.14, 3.19
  1601. BASIC             1, 1.7, 1.11, 3.4, 3.9, 3.10, 3.11, 3.20
  1602. BROWSER           1.15, 2.2, 2.3, 2.9, 3.1, 3.6, 3.7, 3.8, 3.11, 3.12, 3.16,
  1603.                   3.21, 3.22, 4.2, 4.5, 4.6
  1604. C                 1.10, 1.11, 1.12, 1.15, 3.20, 4.1, 4.5
  1605. CACHE             3.6, 3.21
  1606. CERN              3.13
  1607. CGI               0.3, 0.6, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.11,
  1608.                   1.12, 1.14, 1.15, 1.16, 1.17, 1.18, 2.1, 2.2, 2.5, 2.9,
  1609.                   3.2, 3.7, 3.11, 3.12, 3.13, 3.14, 3.15, 3.16, 3.18, 3.20, 3.21, 3.22, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 5.1, 5.2
  1610. CGIWRAP           1.16, 1.17, 3.20, 4.1
  1611. CHMOD             1.16, 3.20
  1612. COOKIE            2.5, 3.12, 3.19
  1613. CREDENTIALS       3.11
  1614. DATABASE          1.15, 2.9, 3.19, 3.20
  1615. DEBUG             4.3, 4.5
  1616. EMAIL             0.3, 3.1, 3.3, 3.4
  1617. ENVIRONMENT       1.7, 2.1, 2.2, 2.9, 3.1, 3.5, 3.6, 3.22
  1618. ERROR             3.4, 4.3, 4.4
  1619. EXPECT            0.5, 2.1, 3.11, 3.20, 4.1
  1620. FAQ               0, 0.3, 0.5, 0.6, 0.7, 1.8, 1.9, 3.4, 3.18, 4.5, 5.1
  1621. FORK              3.8, 3.9
  1622. FRAMES            3.6, 3.15, 3.16, 3.17
  1623. GET               0.1, 0.5, 1.8, 1.18, 2.1, 2.4, 2.9, 3.1, 3.3, 3.11, 3.12,
  1624.                   3.13, 3.16, 3.17, 3.21, 4.2
  1625. HEAD              2.1, 4.2
  1626. HEADER            0.6, 2, 2.1, 2.5, 2.6, 2.9, 3.2, 3.11, 3.13, 3.14, 3.15,
  1627.                   3.21, 4.2, 4.3, 4.4
  1628. HTML              0.3, 1.1, 1.5, 1.7, 1.8, 1.9, 1.17, 1.18, 2.1, 2.2, 3.6,
  1629.                   3.8, 3.9, 3.10, 3.11, 3.14, 3.22, 4, 4.2, 4.3, 4.6, 5.1,
  1630.                   5.2
  1631. HTTP              0.3, 1.1, 1.8, 1.9, 1.15, 1.17, 1.18, 2, 2.1, 2.2, 2.5,
  1632.                   2.6, 2.7, 2.9, 3.4, 3.7, 3.11, 3.12, 3.14, 3.18, 3.21,
  1633.                   3.22, 4.1, 4.2, 4.3, 4.6, 5.1, 5.2
  1634. HTTPD             1.5, 1.6, 1.8, 2.1, 2.5, 2.6, 2.7, 3.7, 3.10, 3.11, 3.13,
  1635.                   3.20, 4.4
  1636. IMAGE             3.17
  1637. JAVA              1.4, 1.7, 3.9
  1638. JAVASCRIPT        3.1, 3.7, 3.16
  1639. LOCATION          2.5, 2.7, 3.13, 4.3
  1640. MICROSOFT         1.5, 3.6
  1641. MOZILLA           3.6
  1642. MULTIPART         1.18
  1643. NCSA              1.1, 1.5, 1.8, 2.2, 2.3, 2.8, 3.1, 5.2
  1644. NETSCAPE          2.5, 3.1, 3.6, 3.7, 3.15
  1645. NOBODY            1.16, 3.20
  1646. NPH               2, 2.5, 2.6, 2.7, 2.8, 3.11, 3.14
  1647. PASSWORD          2.4, 3.11
  1648. PERL              1.10, 1.11, 1.12, 1.15, 1.16, 3.4, 3.16, 3.18, 3.20, 4.1,
  1649.                   5.1
  1650. PERMISSIONS       1.16, 1.17, 4.3
  1651. PHP               1.5, 1.7
  1652. POST              0.5, 0.6, 1.18, 2.1, 2.9, 3.12, 3.22
  1653. PRAGMA            3.21
  1654. REDIRECT          2.5, 3.13
  1655. REFRESH           2.5
  1656. REQUEST           2.1, 2.2, 2.9, 3.9, 3.13, 3.17, 3.21, 4.2
  1657. RESPONSE          1.5, 1.12, 2.1, 2.5, 2.6, 3.9, 3.11, 3.13, 3.17, 3.19,
  1658.                   3.22, 4.2
  1659. SECURITY          1.8, 1.9, 1.16, 1.17, 3.20, 5.1
  1660. SERVER            0.3, 1.4, 1.5, 1.7, 1.8, 1.14, 1.15, 1.16, 1.17, 2.1,
  1661.                   2.2, 2.3, 2.5, 2.8, 2.9, 3.5, 3.9, 3.11, 3.12, 3.16,
  1662.                   3.20, 3.21, 3.22, 4.2, 4.3
  1663. SSI               1.5, 1.7
  1664. STATUS            2.5, 2.7, 3.11, 3.14, 4.3
  1665. TCL               1.11, 1.12, 3.20, 4.1
  1666. UNIX              1.10, 1.16, 2.2, 3.8, 4.5
  1667. URL               0.3, 1.8, 1.17, 2.9, 3.12, 3.13, 3.16, 3.17, 3.21, 3.22
  1668. URLENCODE         3.13
  1669. WWW               0.3, 0.6, 1.4, 1.8, 1.9, 1.11, 1.17, 2.7, 2.9, 3.4, 3.11,
  1670.                   4.1, 4.2, 5.1, 5.2
  1671. .
  1672.