CGI for collecting phone numbers

F

ferreira

I would like to have my visitors sign up for text message updates. What
I need is to have the form submit to a .txt file on the server. All I
need is a list of phone numbers. Does anyone have the cgi or pl code
that would accomplish this task?
 
B

Bart Van der Donck

I would like to have my visitors sign up for text message updates. What
I need is to have the form submit to a .txt file on the server. All I
need is a list of phone numbers. Does anyone have the cgi or pl code
that would accomplish this task?

I would say the most common approach is to have three files:

1. your HTML input form
2. the .pl file that receives the sent data
3. the .txt file the phone number is written to
 
F

ferreira

Bart,
Thank you for the response. I have the format figured out but the code
in the .pl file has me going in circles. Every time I hit the submit
button I get an internal server error.
 
P

Paul Lalli

Thank you for the response. I have the format figured out but the code
in the .pl file has me going in circles. Every time I hit the submit
button I get an internal server error.

Do I understand correctly that you're asking us to write alternative
code for you, because the code you've written yourself "doesn't work"?
That's generally not how this group works.

First, read:
perldoc -q 500
which tells you a little bit about Internal Server Error

Then read your server's logs to see the *real* error message.

Then try running your script on the command line to see if the error
message relates to Perl or to CGI

Finally, if you still can't figure out your problem - and you're sure
it's with Perl, not with CGI - post a short-but-complete script here
that demonstrates the failure.

Paul Lalli
 
T

Tad McClellan

Thank you for the response. I have the format figured out but the code
in the .pl file has me going in circles. Every time I hit the submit
button I get an internal server error.


You have an error on line 17.
 
F

ferreira

Please don't think that I am asking some one to do this for me! I have
spent hours trying to figure out why this simple script won't work.

This is what I have:

The html file:

<FORM ACTION="/sms.pl" METHOD="POST">Cell Number: <INPUT TYPE="text"
NAME="phone"><input type="submit" name="submitButtonName" border="0">
</FORM>


The sms.pl file:

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
my $file = '/users/web/king/web/sms.txt';
open (FILE, ">>" . $file) or die "cannot open file for appending: $!";
flock (FILE, 2) or die "cannot lock file exclusively: $!";
print "Content-type: text/html\n\n";
print FILE $phone . "\n";
close (FILE) or die "cannot close file: $!";



And a sms.txt file
All of the files are in the same place on the server

The is the error log message:
access:
c-68-35-76-89.hsd1.nm.comcast.net - - [09/Jun/2006:17:28:30 +0000]
"POST /sms.pl HTTP/1.1" 500 -

error:
[Fri Jun 9 17:28:30 2006] [error] [client 68.35.76.89] Premature end
of script headers: /users/web/king/web/sms.pl
[Fri Jun 9 17:28:30 2006] [error] [client 68.35.76.89] File does not
exist: /users/web/king/web/error500.html

I have also tried changing the .pl to .cgi but the result is the same.


The error after clicking the submit button:

Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.

Please contact the server administrator, (e-mail address removed) and inform
them of the time the error occurred, and anything you might have done
that may have caused the error.

More information about this error may be available in the server error
log.

Additionally, a 404 Not Found error was encountered while trying to use
an ErrorDocument to handle the request.
 
T

Tad McClellan

This is what I have:
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
my $file = '/users/web/king/web/sms.txt';
open (FILE, ">>" . $file) or die "cannot open file for appending: $!";
flock (FILE, 2) or die "cannot lock file exclusively: $!";
print "Content-type: text/html\n\n";
print FILE $phone . "\n";
close (FILE) or die "cannot close file: $!";


You have never given $phone a value!
 
P

Paul Lalli

Please don't think that I am asking some one to do this for me!

Excuse me? Since you neglected to do so, allow me to quote your
original message.

That's not asking people to do this for you?
I have
spent hours trying to figure out why this simple script won't work.

And yet you chose not to post the script that you spent hours on, and
asked people to write one for you instead. That's asking people to do
it for you.
This is what I have:

#!/usr/bin/perl

You're not using strict. You're not using warnings. You're not using
the CGI module. WHY?
use CGI::Carp qw(fatalsToBrowser);
my $file = '/users/web/king/web/sms.txt';
open (FILE, ">>" . $file) or die "cannot open file for appending: $!";
flock (FILE, 2) or die "cannot lock file exclusively: $!";
print "Content-type: text/html\n\n";

So if either of those system calls fails, you're going to die before
printing out the HTML header? Does that really make sense to you?
print FILE $phone . "\n";
close (FILE) or die "cannot close file: $!";

And a sms.txt file
All of the files are in the same place on the server

The is the error log message:
access:
c-68-35-76-89.hsd1.nm.comcast.net - - [09/Jun/2006:17:28:30 +0000]
"POST /sms.pl HTTP/1.1" 500 -

error:
[Fri Jun 9 17:28:30 2006] [error] [client 68.35.76.89] Premature end
of script headers: /users/web/king/web/sms.pl

Now why do you think you might be getting that? Could it be that your
script is die()ing before printing out the headers?

What happened when you ran this script on the command line, as I asked
you to do in the previous message? Or did you ignore that part of my
post too?
[Fri Jun 9 17:28:30 2006] [error] [client 68.35.76.89] File does not
exist: /users/web/king/web/error500.html

This indicates that your webserver is misconfigured. It knows it
should be printing out the HTTP 500 message to the browser, but the
file containing that message does not exist. This, of course, has
nothing to do with Perl.
I have also tried changing the .pl to .cgi but the result is the same.

What made you think that would solve the problem? It's possible your
webserver is configured to only execute Perl scripts ending in .cgi,
but if it is, that has nothing to do with Perl, and there was nothing
in the error log suggesting that to be the case.

Paul Lalli
 
G

Gunnar Hjalmarsson

Paul said:
So if either of those system calls fails, you're going to die before
printing out the HTML header? Does that really make sense to you?

To me it makes sense, since "fatalsToBrowser" is enabled.
error:
[Fri Jun 9 17:28:30 2006] [error] [client 68.35.76.89] Premature end
of script headers: /users/web/king/web/sms.pl

Now why do you think you might be getting that? Could it be that your
script is die()ing before printing out the headers?

No.
 
B

Brian Wakem

Please don't think that I am asking some one to do this for me! I have
spent hours trying to figure out why this simple script won't work.

This is what I have:

The html file:

<FORM ACTION="/sms.pl" METHOD="POST">Cell Number: <INPUT TYPE="text"
NAME="phone"><input type="submit" name="submitButtonName" border="0">
</FORM>


The sms.pl file:

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
my $file = '/users/web/king/web/sms.txt';
open (FILE, ">>" . $file) or die "cannot open file for appending: $!";
flock (FILE, 2) or die "cannot lock file exclusively: $!";
print "Content-type: text/html\n\n";
print FILE $phone . "\n";
close (FILE) or die "cannot close file: $!";



And a sms.txt file
All of the files are in the same place on the server

The is the error log message:
access:
c-68-35-76-89.hsd1.nm.comcast.net - - [09/Jun/2006:17:28:30 +0000]
"POST /sms.pl HTTP/1.1" 500 -


It is unlikely your webserver is configured to execute files in your
document root. You may have to move it into /cgi-bin or similar, and make
sure it is executable by the webserver (permissions).
 
F

ferreira

Brian,
Your suggestion was one of the problems, a permission.
Pual,
Do you just like to badger newbes or what? I don't even get paid for
this. I'm just a volunteer trying to help out for a good cause.
I was able to find the missing piece and it works perfectly now.
Here is the final code. Maybe someone wanting to submit phone numbers
from a form to a text file with a cgi will run across this post and
save the hassle I went through.

#!/usr/bin/perl -w
use CGI;
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;}
}
my $file = 'sms.txt';
my $phone = $FORM{phone};
open (FILE, ">>" . $file) or die "cannot open file for appending: $!";
flock (FILE, 2) or die "cannot lock file exclusively: $!";
print "Content-type: text/html\n\n";
print FILE $phone . "\n";
close (FILE) or die "cannot close file: $!";
print qq~
<meta HTTP-EQUIV="REFRESH" content="5; url=http://www.GaryKing.org/">
<center><B>Number successfully added!
</B>
<br>
You will be returned to GaryKing .org in 5 seconds.
<p><a href="http://www.GaryKing.org/"> Or click here to return to
GaryKing.org now.</a></p></center>
~;
 
T

Tad McClellan

Pual,
Do you just like to badger newbes or what?


You have not correctly identified the nature of Paul's followups.

It was not badgering, it was letting you in on the social dynamic
of this group of people that you just entered.

You've done a half dozen things that will be seen as rudeness by
many here.

One (probably too) common response to such a post is to killfile
your address, and ignore you forevermore, with you not even
knowing what has happened!

Another response is to make a followup pointing out these things
to you so that you would become aware of them before it is too late.

Which approach is more helpful to you?

Leaving you in ignorance, or helping you to learn what you
need to know to participate fully?


You were walking around with some toilet paper stuck to your shoe.
Many people just snickered at you, Paul was courteous enough to
say "Hey, you have some TP stuck to your shoe".

Now you are acting as if you _prefer_ not knowing.

I don't even get paid for
this.


Neither do we.

So we are not likely to write complete programs to specification,
that is what we do at work.

We don't write programs here. We help people write programs here.

I'm just a volunteer trying to help out for a good cause.


So are we.

(But "help out" is not the same as "do it for me".)

(and if your code leads to embarrassment for your client that makes
the 6 o'clock news, your "help out" may end up costing far more
than what was paid for the code.)

Here is the final code.


I sure hope not...

Maybe someone wanting to submit phone numbers
from a form to a text file with a cgi will run across this post and


not use the code in a cargo-cult manner, but will instead
work to understand what it does. (and then probably decide
to do it differently.)

save the hassle I went through.


I expect the root cause of your hassle was being new to four
different things all at the same time.

1) Usenet

2) web (CGI) programming

3) Perl

4) programmming itself (IMO)


Lurking or reading the Posting Guidelines or even googling
for "netiquette" would handle most of #1. (or simply listening
to the advice that Paul gave you.)

You can start on #2 with:

perldoc -q CGI

Where can I learn about CGI or Web programming in Perl?

What is the correct form of response from a CGI script?

My CGI script runs from the command line but not the browser.
(500 Server Error)

How can I get better error messages from a CGI program?

How do I make sure users can't enter values into a form that
cause my CGI script to do bad things?

How do I decode a CGI form?

You can start on #3 at learn.perl.org, and finish with asking
questions about Perl programming right here.

Learning industry standard programming practices (#4) is not
so easily addressed.

#!/usr/bin/perl -w

use warnings;

is a much better method of enabling warnings.


You should also enable strictures:

use strict;

to get perl to help you find common bugs.



Why did you include this module?

This is not a rhetorical question.

You never make use of any of the facilities that it provides,
so it looks like you are engaging in "cargo cult programming"
(using code even when you do not understand what it does).

Your programming life will be very hard unless we can
successfully teach you to understand code before using it. :)

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;}
}
my $phone = $FORM{phone};


If you do it like in the answer to the "How do I decode a CGI form?" FAQ,
then you can replace all of your code above with these 2 lines:

use CGI qw:)standard);
my $phone = param( "phone" );

Not only is it a whole lot shorter, it does not contain the bugs
that your hand-rolled (and cargo-culted I would assume) code has.

flock (FILE, 2) or die "cannot lock file exclusively: $!";
^
^
You should avoid using system-dependent hard coded values like
that, since the numbers can be different on different systems.

You should import the constants from the Fcntl module instead:

use Fcntl qw:)flock);
flock (FILE, LOCK_EX) or die ...

print FILE $phone . "\n";


You don't say what you are going to use this list of phone numbers
for, but in light of #2 above I better mention a couple more things
in case you haven't thought about them yet.

The web is a wild and wooly place. There are people out there that
just cruise around looking for an opportunity to cause trouble.

They will find opportunity aplenty if you put your "final code"
where they can find it.

What will your code do if a user enters this for the "phone" form value:

http://www.pornography.com

??

Do you _want_ to put that into your phone list? Probably not, so
you'd better do some input checking:

die 'does not look like a phone number' unless $phone =~/^[0-9 -]+$/;

(but don't use die() in a real CGI program...)


Yet another thing to consider: what if the user enters all of
his enemy's phone numbers instead of his own phone number?

Be careful out there!



[snip TOFU.
Yet another breach of standard netiquette...
]
 
F

ferreira

Tad,
Thank you for explaining things to me. I am new to groups and solving
problems in this format. I thought that by saying what I was doing up
front, I would spark questions not criticism. I had not thought of
doing some of the things you suggested. I will start working on those
next. Please understand that I was not meaning to offend anyone, just a
dumb ape stumbling in to modern society. One thing that I would like to
know is what "program/method" works best for testing my code? Something
with good debugging? I am running Mac OS X 10.3.9. I have looked at a
few different programs but would like to know what the pros, like you,
use. I know that many on the group said run it at the command line. Is
that the best for a beginner like me or should I look into something
with more idiot proofing.

James

Tad said:
Pual,
Do you just like to badger newbes or what?


You have not correctly identified the nature of Paul's followups.

It was not badgering, it was letting you in on the social dynamic
of this group of people that you just entered.

You've done a half dozen things that will be seen as rudeness by
many here.

One (probably too) common response to such a post is to killfile
your address, and ignore you forevermore, with you not even
knowing what has happened!

Another response is to make a followup pointing out these things
to you so that you would become aware of them before it is too late.

Which approach is more helpful to you?

Leaving you in ignorance, or helping you to learn what you
need to know to participate fully?


You were walking around with some toilet paper stuck to your shoe.
Many people just snickered at you, Paul was courteous enough to
say "Hey, you have some TP stuck to your shoe".

Now you are acting as if you _prefer_ not knowing.

I don't even get paid for
this.


Neither do we.

So we are not likely to write complete programs to specification,
that is what we do at work.

We don't write programs here. We help people write programs here.

I'm just a volunteer trying to help out for a good cause.


So are we.

(But "help out" is not the same as "do it for me".)

(and if your code leads to embarrassment for your client that makes
the 6 o'clock news, your "help out" may end up costing far more
than what was paid for the code.)

Here is the final code.


I sure hope not...

Maybe someone wanting to submit phone numbers
from a form to a text file with a cgi will run across this post and


not use the code in a cargo-cult manner, but will instead
work to understand what it does. (and then probably decide
to do it differently.)

save the hassle I went through.


I expect the root cause of your hassle was being new to four
different things all at the same time.

1) Usenet

2) web (CGI) programming

3) Perl

4) programmming itself (IMO)


Lurking or reading the Posting Guidelines or even googling
for "netiquette" would handle most of #1. (or simply listening
to the advice that Paul gave you.)

You can start on #2 with:

perldoc -q CGI

Where can I learn about CGI or Web programming in Perl?

What is the correct form of response from a CGI script?

My CGI script runs from the command line but not the browser.
(500 Server Error)

How can I get better error messages from a CGI program?

How do I make sure users can't enter values into a form that
cause my CGI script to do bad things?

How do I decode a CGI form?

You can start on #3 at learn.perl.org, and finish with asking
questions about Perl programming right here.

Learning industry standard programming practices (#4) is not
so easily addressed.

#!/usr/bin/perl -w

use warnings;

is a much better method of enabling warnings.


You should also enable strictures:

use strict;

to get perl to help you find common bugs.



Why did you include this module?

This is not a rhetorical question.

You never make use of any of the facilities that it provides,
so it looks like you are engaging in "cargo cult programming"
(using code even when you do not understand what it does).

Your programming life will be very hard unless we can
successfully teach you to understand code before using it. :)

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;}
}
my $phone = $FORM{phone};


If you do it like in the answer to the "How do I decode a CGI form?" FAQ,
then you can replace all of your code above with these 2 lines:

use CGI qw:)standard);
my $phone = param( "phone" );

Not only is it a whole lot shorter, it does not contain the bugs
that your hand-rolled (and cargo-culted I would assume) code has.

flock (FILE, 2) or die "cannot lock file exclusively: $!";
^
^
You should avoid using system-dependent hard coded values like
that, since the numbers can be different on different systems.

You should import the constants from the Fcntl module instead:

use Fcntl qw:)flock);
flock (FILE, LOCK_EX) or die ...

print FILE $phone . "\n";


You don't say what you are going to use this list of phone numbers
for, but in light of #2 above I better mention a couple more things
in case you haven't thought about them yet.

The web is a wild and wooly place. There are people out there that
just cruise around looking for an opportunity to cause trouble.

They will find opportunity aplenty if you put your "final code"
where they can find it.

What will your code do if a user enters this for the "phone" form value:

http://www.pornography.com

??

Do you _want_ to put that into your phone list? Probably not, so
you'd better do some input checking:

die 'does not look like a phone number' unless $phone =~/^[0-9 -]+$/;

(but don't use die() in a real CGI program...)


Yet another thing to consider: what if the user enters all of
his enemy's phone numbers instead of his own phone number?

Be careful out there!



[snip TOFU.
Yet another breach of standard netiquette...
]
 
S

Sherm Pendley

Jim Gibson said:
Print the HTTP header here, before you do anything else. If open fails
in the next line, you will be sending invalid HTML to the server.

CGI:Carp should take care of that. It overrides die() and warn() with versions
that send headers before their output.

sherm--
 
S

Sherm Pendley

Paul Lalli said:
So if either of those system calls fails, you're going to die before
printing out the HTML header? Does that really make sense to you?

Makes sense to me. CGI::Carp overrides the built-in die() and warn() functions
with versions that print a basic set of HTTP headers before their messages.
[Fri Jun 9 17:28:30 2006] [error] [client 68.35.76.89] Premature end
of script headers: /users/web/king/web/sms.pl

Now why do you think you might be getting that? Could it be that your
script is die()ing before printing out the headers?

If so, that narrows the problem considerably - it has to be die()ing before
CGI::Carp is being use()d.

sherm--
 
S

Sherm Pendley

I was able to find the missing piece and it works perfectly now.
Here is the final code. Maybe someone wanting to submit phone numbers
from a form to a text file with a cgi will run across this post and
save the hassle I went through.

#!/usr/bin/perl -w
use CGI;
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 only purpose the above has is to serve as a horrible example of hand-
written CGI parsing.

Do yourself a favor - buy a good Perl book. Read it.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top