Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

CGI Question

by Irishboy24 (Acolyte)
on Jul 20, 2009 at 17:14 UTC ( #781691=perlquestion: print w/ replies, xml ) Need Help??
Irishboy24 has asked for the wisdom of the Perl Monks concerning the following question:

Hey Guys, i am new to perl CGI scripting. i am running apache on localhost as my web server. i tested it with a sample html script on the local host and it works. i wrote a small CGI script and tried to run it on the local host using apache and it doesnt work. it just prints the entire code on the webbrowser (IE8). my Document root on Apache is "C:\Documents and Settings\Maddy17\My Documents\mywebsite". All my cgi scripts are stored here.
here is the code
#!/usr/bin/perl use CGI; my $cgi = new CGI; #set up the partial header , Content-type print $cgi->header("text/html"); #create title print $cgi->start_html("LOGIN PAGE"); print $cgi->h1("ENTER USERID"); print $cgi->p("USERID:"); print $cgi->textfield(-name=> "ID"); print $cgi->p("PASSWORD:"); print $cgi->password_field(-name=> "PASSWD"); print $cgi->hr(); print $cgi->submit(-name=> "enter", -value=> "Enter"); print $cgi->submit(-name=> "cancel", -value=> "Cancel"); print $cgi->end_html();
Please let me know if i am missing something. thanks.

Comment on CGI Question
Download Code
Re: CGI Question
by gulden (Monk) on Jul 20, 2009 at 17:23 UTC
    You need to configure Apache to handle that files. The following AddHandler directive tells the server to treat all files with the cgi or pl extension as CGI programs:
    AddHandler cgi-script cgi pl
    After putting your CGI running read about mod_perl for better performance.
    A contentious debate is always associated with a lack of valid arguments.
      Hey Thank you for the quick reply. i checked the httpd.conf file and surel +y enough the "Addhandler" was commented out. i added ur option and th +en tunrned on CGI in the options directive. I restarted Apache and th +en launched the browser. now it gives me an internal server error pro +blem. i went into the apache logs and this is the error it shows "The system cannot find the file specified. : couldn't spawn child p +rocess: C:/Documents and Settings/Maddy17/My Documents/mywebsite/env. +cgi" Now i went back to the document root dir and checked if the file was p +resent. the file exists there so i am not sure what to do now.
        Could you run the cgi on the cmd line?
        Make sure the path to perl is correct.

        If Apache can't find the file you may have either a permissions problem, a problem in your config file, or some bad .htaccess directives.

        • Does the Apache server have the right to view files in the directory where the symbolic link resides? If not, the file will be invisible to Apache even though you can see it.
        • How have you mapped server URLs to file system paths? Check your mod_rewrite directives. Are they doing what you think they do? You can monitor the rewrite process by looking at the log files (see the RewriteLog and RewriteLogLevel directive). Look also for Location, LocationMatch, Files and FilesMatch directives. You should also check mod_alias directives, in particular, ScriptAlias and ScriptAliasMatch. mod_alias and the other directives all tell Apache how to convert URLs to system file paths.
        • Is the file symbolically linked to some other file elsewhere on the system? If so, you will either need to insure that FollowSymLinks is turned on for that directory or to replace the symbolic link with an actual copy. The more secure solution is to copy the file. Turning on symbolic links makes you more vulnerable to certain kinds of malicious attacks.

        Best, beth

Re: CGI Question
by CountZero (Bishop) on Jul 20, 2009 at 17:28 UTC
    Most certainly your Apache-configuration is not correct. Did you read the Apache manual? The tutorial on "Dynamic Content with CGI" will explain how to make Apache execute your Perl-scripts.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: CGI Question
by jrsimmon (Hermit) on Jul 20, 2009 at 17:52 UTC
    If Apache is set up correctly (assuming it's a current install and you selected the defaults, it is), the problem may be that you are printing something before you print the header. I was recently bit by this bug when I forgot to remove a debug print statement from a script.

    You can test whether this is the case by running the cgi script from the command line and ensuring that the first output from the script is/isn't the text/html header.
Re: CGI Question
by Anonymous Monk on Jul 20, 2009 at 18:26 UTC
    On windows, apache will try to execute shebang , in your example /usr/bin/perl change it to #!perl --
      On windows, apache will try to execute shebang

      ... unless / until you set ScriptInterpreterSource to Registry (or Registry-Strict in Apache >= 2.0) in the httpd.conf.


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: CGI Question
by targetsmart (Curate) on Jul 21, 2009 at 10:54 UTC
    This Troubleshooting Perl CGI scripts may help you.

    -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.
      That is a good guide, but his problem was apache config, and he already solved it. Thanks
Re: CGI Question
by linuxer (Deacon) on Jul 21, 2009 at 19:08 UTC

    You should consider to use start_form() and end_form() from CGI to start and finish your HTML form.

    You can save many prints if you print a list of $cgi->... elements.

    print $cgi->header(), $cgi->start_html('title'), $cgi->start_form(), #... your form here $cgi->end_form(), $cgi->end_html(), ;

    Update: Maybe you should have a look at a templating system like HTML::Template as well. Using a templating system helps you to separate your HTML content from your perl code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://781691]
Approved by kyle
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-09-20 10:07 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (158 votes), past polls