Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cgi perl script problem

Status
Not open for further replies.

airhead1

Programmer
Jun 30, 2004
11
US
im new to perl and cgi allthgether, im gona get a book about it, but I need to have a script that can take form input and put it into a file on my server here is the code I have sofar

#!perl
print "Content-type:text/html\n\n";
# Get Browser Verion Info


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;
}
open(fileOUT, ">>F:/apache/pass.txt") or dienice("Can't open log.txt for writing: $!");
flock(fileOUT, 2);
seek(fileOUT, 0, 2);
print fileOUT "$pass{pass} :\n";
print fileOUT "$FORM{login} :\n";
close(fileOUT);
}


the html part has password and login values which i want to be posted to a file, the NAME= value for the form is login and pass
im having trouble finding out what to enter to get the values to be posted into the file, please help, thanks
 
I've added the bits in [blue]blue[/blue] to simulate some data coming in from POST. The bits in [red]red[/red] are amendments to your original post.

Code:
#!perl

[blue]$ENV{'REQUEST_METHOD'} = 'POST';[/blue]

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

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    
    [blue]$buffer = 'login=myLogin&pass=myPassword';[/blue]
    
    @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;
    }

open(fileOUT, ">>F:/apache/pass.txt");

flock(fileOUT, 2);
seek(fileOUT, 0, 2);

[red]print fileOUT "password : $FORM{pass}\n";
print fileOUT "login : $FORM{login}\n";[/red]

close(fileOUT);

}


Kind Regards
Duncan
 
Code:
#!/usr/bin/perl

use CGI;

$pass=param('pass');
$login=param('login');

open FILE, ">>F:/Apache/pass.txt";
print FILE "$login --> $pass\n";
close FILE;
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
i got a internal server error, the log said "Premature end of script headers: f:/apache/cgi-bin/passget.cgi"

also, are you sure this is the right part to take input from the form?
$FORM{pass}

the html part is
"
<form TARGET="_top" name="passget" action="/cgi-bin/passget.cgi" METHOD="post" enctype="text/plain">
<input type="hidden" name="login">
<input type="hidden" name="passwd">
"

i don`t get y im having this error
 
You're getting the error because you're not outputting the correct headers, and Apache is quite unforgiving in this case.

Code:
#!/usr/bin/perl

use CGI;

$pass=param('pass');
$login=param('login');

open FILE, ">>F:/Apache/pass.txt";
print FILE "$login --> $pass\n";
close FILE;
print header; #this is from the CGI module, the same as
print "Content-Type: text/html/n/n";

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
it gets errors

Premature end of script headers: f:/apache/cgi-bin/passget1.cgi
Undefined subroutine &main::param called at f:\apache\cgi-bin\passget1.cgi line 5.
 
all i want is a simple script to write form input, with a value of pass, and login, to a file, thats all
 
ok, so i went back to using:
"
#!perl

$ENV{'REQUEST_METHOD'} = 'POST';

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;
}

open(fileOUT, ">>F:/apache/pass.txt");

flock(fileOUT, 2);
seek(fileOUT, 0, 2);

print fileOUT "password : $FORM{$pass}\n";
print fileOUT "login : $FORM{$login}\n";

close(fileOUT);

}
"
and i get this error in the error log "Premature end of script headers: f:/apache/cgi-bin/passget1.cgi"

the

password :
login :

part shows up in pass.txt, but not the form values, why?

i also tried
print fileOUT "password : $FORM{pass}\n";
print fileOUT "login : $FORM{login}\n";

but it still doesn`t work, those are the value names in the form, why don`t they show up?
 
airhead

you ask me "are you sure this is the right part to take input from the form?
$FORM{pass}"


look at your code dude:-

Code:
#!perl
print "Content-type:text/html\n\n";
# Get Browser Verion Info
    

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;
        [red][b]$FORM{$name} = $value;[/b][/red]
    }
open(fileOUT, ">>F:/apache/pass.txt") or dienice("Can't open log.txt for writing: $!");
flock(fileOUT, 2);
seek(fileOUT, 0, 2);
print fileOUT "$pass{[red][b]pass[/b][/red]} :\n";
print fileOUT "$FORM{login} :\n";
close(fileOUT);
}

... and now you show the HTML code it clearly says passwd

you;ll have to be a little more careful when posting the question for us to try to help :)


Kind Regards
Duncan
 
ya, i changed the passwd later, to pass in the html part, so everything was the same but i still have problems
 
show us what you have now?
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Dude

You must print a header:-

#!perl

[red]print "Content-type: text/html\n\n";[/red]

$ENV{'REQUEST_METHOD'} = 'POST';



Kind Regards
Duncan
 
so far i have

#!perl

print "Content-type: text/html\n\n";

$ENV{'REQUEST_METHOD'} = 'POST';

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;
}

open(fileOUT, ">>F:/apache/pass.txt");

flock(fileOUT, 2);
seek(fileOUT, 0, 2);

print fileOUT "password : $FORM{$pass}\n";
print fileOUT "login : $FORM{$login}\n";

close(fileOUT);

}

yes, print "Content-type: text/html\n\n"; fixed the header error
but in the pass.txt file, it only says
password :
login :
no matter what i type, nothing shows up in the txt file
 
as for your code, paul, it keeps getting a "Undefined subroutine &main::param called at f:\apache\cgi-bin\passget1.cgi line 7." error in the log
 
dude

just try replacing with this line-

open(fileOUT, ">>F:\apache\pass.txt");

or...

open(fileOUT, ">>F:\\apache\\pass.txt");

or...

open(fileOUT, ">>F://apache//pass.txt");

I can't remember the directory rules for the PC ;-)


Kind Regards
Duncan
 
dude
thats not the problem, it CAN write to the file, and it writes the first part, but NOT the VALUES of the form
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top