+ Reply to Thread
Results 1 to 6 of 6

Thread: Perl CGI script

  1. #1
    iainc is offline x10Hosting Member iainc is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    3

    Perl CGI script

    im trying to run an form-to-email script:

    #!/usr/bin/perl


    # IMPORTANT: MAKE SURE THESE TWO VALUES ARE SET CORRECTLY FOR YOU!
    $mailprog= "/usr/sbin/sendmail" ;
    $recipient= "administrator\@pung*****.com" ; # make sure to \ escape the @

    # Get the CGI input variables
    %in= &getcgivars ;

    # Open the mailing process
    open(MAIL, "|$mailprog $recipient")
    || &HTMLdie("Couldn't send the mail (couldn't run $mailprog).") ;

    # Print the header information
    $ENV{'HTTP_REFERER'} || ($ENV{'HTTP_REFERER'}= "your Web site") ;
    print MAIL "Subject: Form data from the Web\n\n",
    "The following data was entered at $ENV{'HTTP_REFERER'}:\n\n" ;


    # Find length of longest field name, for formatting; include space for colon
    $maxlength= 0 ;
    foreach (keys %in) {
    $maxlength= length if length > $maxlength ;
    }
    $maxlength++ ;

    # Print each CGI variable received by the script, one per line.
    # This just prints the fields in alphabetical order. To define your own
    # order, use something like
    # foreach ('firstname', 'lastname', 'phone', 'address1', ... ) {
    foreach (sort keys %in) {

    # If a field has newlines, it's probably a block of text; indent it.
    if ($in{$_}=~ /\n/) {
    $in{$_}= "\n" . $in{$_} ;
    $in{$_}=~ s/\n/\n /g ;
    $in{$_}.= "\n" ;
    }

    # comma-separate multiple selections
    $in{$_}=~ s/\0/, /g ;

    # Print fields, aligning columns neatly
    printf MAIL "%-${maxlength}s %s\n", "$_:", $in{$_} ;
    }


    # Close the process and mail the data
    close(MAIL) ;


    # Print an HTML response to the user
    print <<EOF ;
    Content-type: text/html

    <html>
    <body>
    <h3>Your data has been sent.</h3>
    </body>
    </html>
    EOF

    exit ;


    #-------------- start of &getcgivars() module, copied in -------------

    # Read all CGI vars into an associative array.
    # If multiple input fields have the same name, they are concatenated into
    # one array element and delimited with the \0 character (which fails if
    # the input has any \0 characters, very unlikely but conceivably possible).
    # Currently only supports Content-Type of application/x-www-form-urlencoded.
    sub getcgivars {
    local($in, %in) ;
    local($name, $value) ;


    # First, read entire string of CGI vars into $in
    if ( ($ENV{'REQUEST_METHOD'} eq 'GET') ||
    ($ENV{'REQUEST_METHOD'} eq 'HEAD') ) {
    $in= $ENV{'QUERY_STRING'} ;

    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i) {
    length($ENV{'CONTENT_LENGTH'})
    || &HTMLdie("No Content-Length sent with the POST request.") ;
    read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;

    } else {
    &HTMLdie("Unsupported Content-Type: $ENV{'CONTENT_TYPE'}") ;
    }

    } else {
    &HTMLdie("Script was called with unsupported REQUEST_METHOD.") ;
    }

    # Resolve and unencode name/value pairs into %in
    foreach (split(/[&;]/, $in)) {
    s/\+/ /g ;
    ($name, $value)= split('=', $_, 2) ;
    $name=~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge ;
    $value=~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge ;
    $in{$name}.= "\0" if defined($in{$name}) ; # concatenate multiple vars
    $in{$name}.= $value ;
    }

    return %in ;

    }


    # Die, outputting HTML error page
    # If no $title, use a default title
    sub HTMLdie {
    local($msg,$title)= @_ ;
    $title= "CGI Error" if $title eq '' ;
    print <<EOF ;
    Content-type: text/html

    <html>
    <head>
    <title>$title</title>
    </head>
    <body>
    <h1>$title</h1>
    <h3>$msg</h3>
    </body>
    </html>
    EOF

    exit ;
    }

    #-------------- end of &getcgivars() module --------------------------


    it is not working and i get the internal 500 error

    i've tryed many solutions include just putting the fail safe:
    #!/usr/bin/perl
    print "Content-type: text/plain\n\n";
    print "testing...\n";

    . this still comes up as a 500 error.

    i have named it mailer.pl and was wondering how to fix the 500 error

  2. #2
    Join Date
    Aug 2007
    Location
    Gangstas Paradise
    Posts
    4,143

    Re: Perl CGI script

    First, what folder are you placing this in. It is a cgi-script and should go in your "public_html/cgi-bin". Then chkmod its permissions to 755.

    Also you should put wT after the hash bang.
    eg : #!/usr/bin/perl wT

    http://dev.x10hosting.com (this has nothing to do with x10hosting)

    ->All us helpful people here at x10hosting would like to reach our next user groups, "Community Paragon". Please click the +rep icon on the left hand side of a post if that post was helpfull.



  3. #3
    Join Date
    Aug 2007
    Location
    Gangstas Paradise
    Posts
    4,143

    Re: Perl CGI script

    First, what folder are you placing this in. It is a cgi-script and should go in your "public_html/cgi-bin". Then chkmod its permissions to 755.

    Also you should put -wT after the hash bang.
    eg : #!/usr/bin/perl -wT
    Last edited by DefecTalisman; 12-08-2007 at 04:38 AM.

    http://dev.x10hosting.com (this has nothing to do with x10hosting)

    ->All us helpful people here at x10hosting would like to reach our next user groups, "Community Paragon". Please click the +rep icon on the left hand side of a post if that post was helpfull.



  4. #4
    FengFeng's Avatar
    FengFeng is offline x10Hosting Member FengFeng is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    China,Canton
    Posts
    59

    Re: Perl CGI script

    why we put wT after the hash bang??

  5. #5
    Join Date
    Aug 2007
    Location
    Gangstas Paradise
    Posts
    4,143

    Re: Perl CGI script

    Edited post above (-wT)

    The w is to produce warnings to help debug.

    The T has totally escaped me, but it is for a more secure script (only works when uploaded, well not on my localhost anyways)

    http://dev.x10hosting.com (this has nothing to do with x10hosting)

    ->All us helpful people here at x10hosting would like to reach our next user groups, "Community Paragon". Please click the +rep icon on the left hand side of a post if that post was helpfull.



  6. #6
    max1x is offline x10Hosting Member max1x is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    27

    Re: Perl CGI script

    Just an FYI, all the perl scripts should use the follwoing modules:

    use strict;
    use warning;
    use diagnostics; These are core for 5.8.x, so should be avaliable on the newer version of Linux (Fedora or utunbu, RedHat)|| Unix (solaris 10, etc)OS's.

+ Reply to Thread

Similar Threads

  1. perl script for guestbook, need help
    By shifeng in forum Scripts & 3rd Party Apps
    Replies: 14
    Last Post: 01-06-2008, 12:31 PM
  2. Replies: 8
    Last Post: 12-03-2007, 04:12 PM
  3. To all those using Perl and CGI
    By DefecTalisman in forum Free Hosting
    Replies: 0
    Last Post: 09-16-2007, 03:56 AM
  4. Perl script execution?
    By ArNoX in forum Free Hosting
    Replies: 1
    Last Post: 09-15-2007, 06:02 PM
  5. Server UP time Script
    By dharmil in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 04-03-2006, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers