Follow us on...
Follow us on Twitter Follow us on Facebook
Register
Page 1 of 2 12 LastLast
Results 1 to 8 of 12
  1. #1
    simX's Avatar
    simX is offline Unofficial Mac Genius
    Join Date
    Sep 2001
    Location
    Bay Area, CA
    Posts
    2,185
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Getting a CGI script to work locally on my Mac


    I've been trying to make a perl CGI script for my website, but I don't have any UNIX server to test it on, so I decided I might as well just test in on my iBook since it has perl installed.

    The problem is that when I set it up (I'm following the www.htmlgoodies.com CGI primers tutorial), it doesn't work correctly. In OmniWeb and Mozilla, when the HTML page accesses the cgi script, it just displays the CGI script contents on the page instead of running the script.

    I can't figure out what the problem is: I made sure that the path to perl was correct, and that the cgi script was executable, but I still cannot make it work. Is there any way to get this to run locally?

    <!-- Here's the HTML page (basically directly from the HTMLgoodies.com tutorial):

    <HTML>
    <HEAD>
    <TITLE>
    Guestbook Script
    </TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF">
    <FORM METHOD="post" ACTION="cgi-bin/guestbook.pl">
    <INPUT NAME="name" SIZE=50 TYPE="text">
    <B>Your Name</B><BR>
    <INPUT NAME="email" SIZE=50 TYPE="text">
    <B>Your E-Mail Address</B><BR>
    <INPUT TYPE="hidden" NAME="submitaddress" VALUE="simX@mac.com">
    <B>Write to me below:</B>
    <P>
    <TEXTAREA NAME="feedback" ROWS=10 COLS=50>
    </TEXTAREA>
    <P>
    <CENTER>
    <INPUT TYPE=submit VALUE="SEND">
    <INPUT TYPE=reset VALUE="CLEAR">
    </CENTER>
    </FORM>
    </BODY>
    </HTML>

    And here's the CGI script:

    #!/usr/bin/perl

    # That is the path to PERL just above It MUST be first in the script

    # The following accepts the data from the form


    if ($ENV{'REQUEST_METHOD'} eq 'POST') {


    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});


    @pairs = split(/&/, $buffer);

    foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $FORM{$name} = $value;
    }



    # The following sends the email

    open (MESSAGE,"| /usr/sbin/sendmail -t");
    # the default for the above is /usr/lib/sendmail

    print MESSAGE "To: $FORM{submitaddress}\n";
    print MESSAGE "From: $FORM{name}\n";
    print MESSAGE "Reply-To: $FORM{email}\n";

    print MESSAGE "Subject: Feedback from $FORM{name} at $ENV{'REMOTE_HOST'}\n\n";
    print MESSAGE "The user wrote:\n\n";
    print MESSAGE "$FORM{feedback}\n";
    close (MESSAGE);

    &thank_you;
    }




    #The following creates the Thank You page display

    sub thank_you {

    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE>Thank You!</TITLE>\n";
    print "</HEAD>\n";
    print "<BODY BGCOLOR=#FFFFCC TEXT=#000000>\n";
    print "<H1>Thank You!</H1>\n";
    print "\n";
    print "<P>\n";
    print "<H3>Your feedback is greatly appreciated.<BR>\n";
    print "<P>\n";
    print "</BODY>\n";
    print "</HTML>\n";
    exit(0);
    } -->

    You can download the HTML code and CGI script here:

    http://homepage.mac.com/simx/.Public/guestbook.html

    http://homepage.mac.com/simx/.Public/guestbook.pl


    I would post it, but Apple's HomePage iTools feature doesn't seem to be running on UNIX (or it blocks CGI scripts from running), so it won't work there. Can anybody see if they can get this to work or show me what's wrong? (And, can anybody tell me if you CAN run cgi scripts on HomePage? That would be awesome.)

    By the way.. how do I stop the forum from automatically interpreting the HTML. I wanted to post the text, not have MacOSX.com interpret it.
    Last edited by simX; July 11th, 2002 at 12:37 PM.
    -- simX

    Get Memory Usage Getter, the only Mac OS X utility that graphically displays the memory usage of your open processes! http://homepage.mac.com/simx/

    450 MHz G4 Cube | 15" flat-panel Apple Studio Display | 896 MB RAM | Que! Fire 12x10x32x FireWire CD-RW | OS X 10.1.5 Build 5S66 | Mac OS 9.2.2 | Telex M-560 Microphone | Epson Stylus Color 777 | TI-Graph Link USB | Pro Speakers/Mouse/Keyboard | Airport card | iPod

    "Some people's minds are like cement: all mixed up and permanently set..." -- Andrew Welch, el Presidente, Ambrosia Software, Inc.

    "You know that first hit of heroin is free." -- Scott McNealy, Sun Microsystem's CEO, on Microsoft's .NET .

    "The day Microsoft makes a product that doesn't suck is the day they start making vacuum cleaners." -- Unknown

  2. #2
    azosx's Avatar
    azosx is offline Banned
    Join Date
    May 2002
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe this URL will help you configure Apache for use with CGI.

    http://httpd.apache.org/docs/howto/cgi.html

    It sounds like you need to install the CGI module to interpret your scripts.

    I'm not familiar with Apache on OS X so perhaps that tutorial will not help.

    If you need further help, I can configure Apache in Linux and see what the problem is exactly.

    Let me know.

  3. #3
    vertigo's Avatar
    vertigo is offline Swollen Member
    Join Date
    Oct 2000
    Location
    Baltimore, MD
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    double check that you have cgi enabled in your httpd.conf file (/etc/httpd/httpd.conf)

    you'll need to add ExecCGI to your Options directive in the appropriate &#60Directory&#62 block. it should look something like this:

    &#60Directory "/Library/WebServer/Documents"&#62
    Options Indexes FollowSymLinks MultiViews ExecCGI
    ...
    &#60/Directory&#62

    you'll also need to make sure the AddHandler line for .cgi is uncommented, it should look like this:

    AddHandler cgi-script .cgi

    then of course restart httpd

    p.s. to get the html brackets, escape them. left is 60, right is 62. check out http://www.bbsinc.com/symbol.html

  4. #4
    simX's Avatar
    simX is offline Unofficial Mac Genius
    Join Date
    Sep 2001
    Location
    Bay Area, CA
    Posts
    2,185
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Isn't it the command-line utility "perl" that interprets the CGI scripts, though? What does Apache have to do with this – I thought that was just a web server, which I don't need since all the files are locally on my hard drive.

    -- simX

    Get Memory Usage Getter, the only Mac OS X utility that graphically displays the memory usage of your open processes! http://homepage.mac.com/simx/

    450 MHz G4 Cube | 15" flat-panel Apple Studio Display | 896 MB RAM | Que! Fire 12x10x32x FireWire CD-RW | OS X 10.1.5 Build 5S66 | Mac OS 9.2.2 | Telex M-560 Microphone | Epson Stylus Color 777 | TI-Graph Link USB | Pro Speakers/Mouse/Keyboard | Airport card | iPod

    "Some people's minds are like cement: all mixed up and permanently set..." -- Andrew Welch, el Presidente, Ambrosia Software, Inc.

    "You know that first hit of heroin is free." -- Scott McNealy, Sun Microsystem's CEO, on Microsoft's .NET .

    "The day Microsoft makes a product that doesn't suck is the day they start making vacuum cleaners." -- Unknown

  5. #5
    vertigo's Avatar
    vertigo is offline Swollen Member
    Join Date
    Oct 2000
    Location
    Baltimore, MD
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    apache isn't interpreting your perl script, perl is (thus the #! line at the top pointing to perl). but apache does have to know when to treat the requested file as a CGI, which is why you need the AddHandler. the ExecCGI option is more of a security thing, since you may not want to allow CGI execution from everywhere. apache also has to pass in environment stuff, and redirect output back to the client, etc. why do you think you can read from STDIN and print to STDOUT and have it all transparently routed to/from the client's browser? gg apache.

  6. #6
    simX's Avatar
    simX is offline Unofficial Mac Genius
    Join Date
    Sep 2001
    Location
    Bay Area, CA
    Posts
    2,185
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hrm.

    I put this at the bottom of the file:

    <Directory /Users/simmy/Sites/smbxas/cgi-bin>
    Options +ExecCGI
    </Directory>

    And I uncommented the AddHandler script so it looks like this:

    AddHandler cgi-script .cgi

    I restarted my Mac (or is there some command I need to do for it to reload the new file?), and it still doesn't work.

    Any more suggestions?

    I still don't understand what Apache has to do with it. Doesn't the browser I use look at the HTML, see that it wants a CGI script, calls the perl command and tells it to execute the specified CGI script, and then perl gives the browser what to display. Where exactly does Apache come in on this?
    -- simX

    Get Memory Usage Getter, the only Mac OS X utility that graphically displays the memory usage of your open processes! http://homepage.mac.com/simx/

    450 MHz G4 Cube | 15" flat-panel Apple Studio Display | 896 MB RAM | Que! Fire 12x10x32x FireWire CD-RW | OS X 10.1.5 Build 5S66 | Mac OS 9.2.2 | Telex M-560 Microphone | Epson Stylus Color 777 | TI-Graph Link USB | Pro Speakers/Mouse/Keyboard | Airport card | iPod

    "Some people's minds are like cement: all mixed up and permanently set..." -- Andrew Welch, el Presidente, Ambrosia Software, Inc.

    "You know that first hit of heroin is free." -- Scott McNealy, Sun Microsystem's CEO, on Microsoft's .NET .

    "The day Microsoft makes a product that doesn't suck is the day they start making vacuum cleaners." -- Unknown

  7. #7
    azosx's Avatar
    azosx is offline Banned
    Join Date
    May 2002
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe I missunderstood but you're trying to serve a webpage from your home computer running OS X right?

    If so, you are serving web pages with the Apache web server built into OS X.

    Apache needs to know how to handle the perl scripts you are trying to incorporate into your website.

    Without the CGI module, that will pass it's function along to Perl, it's just going to serve your perl script as raw data or text.

    It doesn't know how to work with Perl on your computer without the module telling it what to do.

    I don't think what the other user described above will work without the CGI module installed.

  8. #8
    vertigo's Avatar
    vertigo is offline Swollen Member
    Join Date
    Oct 2000
    Location
    Baltimore, MD
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    azosx - the CGI module is installed by default in apache. you can look in httpd.conf and see that mod_cgi is added.

    simx - the ExecCGI should go _inside_ the Directory block, not simply at the end of the file. if you're using pico, hit control-w and search for ExecCGI. the first match should be right below the Directory tag and right above the Options directive. just add ExecCGI to that list. mine looks like this:

    Options Indexes FollowSymLinks MultiViews ExecCGI

    make sure you save the file (you'll need sudo), and just go into the Sharing control panel, turn off web sharing, and turn it back on.

    also, make sure your filename ends with .cgi, not .pl. or add .pl to the AddHandler line if you want. i find .cgi a bit more intuitive.

 

 
Page 1 of 2 12 LastLast

Similar Threads

  1. A bit of nostalgia: A Salute to Mac OS X
    By simX in forum Apple News, Rumors & Discussion
    Replies: 31
    Last Post: March 24th, 2005, 06:45 AM
  2. HP Photosmart 1315 and USB Print Sharing
    By zwheeloc in forum Mac Classic System & Software
    Replies: 12
    Last Post: February 6th, 2003, 08:20 PM
  3. Why buy an Apple?
    By Annihilatus in forum Apple News, Rumors & Discussion
    Replies: 50
    Last Post: August 1st, 2002, 04:11 AM
  4. apps list
    By Mac Osxtopus in forum Mac OS X System & Mac Software
    Replies: 7
    Last Post: May 29th, 2002, 11:31 AM
  5. Apple: Forget XP, try the Mac
    By tagliatelle in forum Bob's Place
    Replies: 1
    Last Post: November 25th, 2001, 06:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •