473,788 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot write from perl CGI to text file

9 New Member
Hi all,

I am new to perl and this forum. I am trying to setup a mailing list subscription functionality for customers to receive a periodic newsletter from me. My perl program grabs the html form 'email address' text input but I am then having issues writing this data to a plain text file.

Below is my html form followed by my perl script. When i run this on IIS, i dont receive any errors, nor does anything get written to the file. Any help would be most appreciated. :)

email.html
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <FORM ACTION="/new/email.pl" METHOD="POST">
  4. Email Address: <INPUT TYPE="text" NAME="email" size=30>
  5. <br><br><INPUT TYPE="submit" VALUE="Sign up">
  6. </FORM>
  7. </body>
  8. </html>
  9.  
email.pl
Expand|Select|Wrap|Line Numbers
  1. print "Content-type: text/html\n\n";
  2.  
  3. #Read email address from Form
  4. if ($ENV{'REQUEST_METHOD'} eq 'POST')
  5.  {
  6. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  7. @pairs = split(/&/, $buffer);
  8. foreach $pair (@pairs)
                  {
  9.        ($name, $value) = split(/=/, $pair);
  10.        $value =~ tr/+/ /;
  11.        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  12.         $FORM{$name} = $value;
  13.     }}  
  14.  
  15. #Write to file
  16. my $file = '/new/email.txt';  
  17.  
  18. open (FILE, ">>" . $file) or die "cannot open file for appending: $!"; 
  19. flock (FILE, 2) or die "cannot lock file exclusively: $!"; 
  20. print FILE $FORM{email};
  21. close (FILE) or die "cannot close file: $!";   
  22.  
Thanks again,

Mick
Oct 15 '07 #1
21 5822
anjigorantla
8 New Member
hi,
try with this simple code,
definitely it will work 4 u,

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <FORM ACTION="/new/email.pl" METHOD="POST">
  4. Email Address: <INPUT TYPE="text" NAME="email" size=30>
  5. <br><br><INPUT TYPE="submit" VALUE="Sign up">
  6. </FORM>
  7. </body>
  8. </html>
  9.  
email.pl
-----------
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI qw(:standard);
  3.  use Fcntl ':flock';
  4. print "Content-type: text/html\n\n";
  5. $a=param('email');
  6. #Write to file
  7. my $file = '/new/email.txt'; 
  8.  
  9. open (FILE, ">>" .$file) or die "cannot open file for appending: $!";
  10. flock (FILE, 2) or die "cannot lock file exclusively: $!";
  11. print FILE $a;
  12. close (FILE) or die "cannot close file: $!";
  13.  
Oct 15 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
You have posted code into the forum without using proper code tags. It is best practice here on TSDN to wrap all code posted into the forum in code tags.

Code tags start with [code] and end with [/code]. If you need an example other than this one, the please refer to the REPLY GUIDELINES (or POSTING GUIDELINES if you are starting a discussion) to the right of the Message window. You will find the examples and suggestions for posting there.

In addition, you can also add the language to your code tags. Here is an example:

[code=perl]
<some code>

[/code]

Please know that I have fixed your posts above to include the proper code tags. Please be sure and use code tags in all of your future posts here on TSDN.

This applies to BOTH of you as neither of you used the tags!!

- Moderator
Oct 15 '07 #3
Mick1000
9 New Member
thanks for your quick response!! unfortunately, this still did not work. :( i ran it, entered an email address and a blank page came up with the status 'done'. still no write was made to the file.

so my .htm form has an email address entered and submitted and the output is a blank page. not sure what is up here. any other suggestions?

im running ActivePerl-5.8.8.820-MSWin32-x86-274739.msi on windows 2003 with IIS 6. dont know if this helps at all.

also, what is html 4 strict? do i need to change anything here?

cheers,

Mick
Oct 15 '07 #4
eWish
971 Recognized Expert Contributor
Well, I would make sure that the path to the file is correct. Then add some code for debuging. I don't believe that flock works on windows. I believe this is unix only.


Here is a script that will help you. I have added some code to check the email address to make sure that is validates. No need for a security hole:)

This code uses Email::Valid
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;
  8. use Email::Valid;
  9.  
  10. my $q = CGI->new;
  11.  
  12. print $q->header; 
  13. print $q->start_html(-title =>'Mysite.com'); 
  14.  
  15. my $email_file = 'h:/path/to/file/email_list.txt';
  16.  
  17.     if (Email::Valid->address($q->param('email'))) {
  18.          &process_data($q->param('email'));
  19.     } else {
  20.         print 'Your email address does not appear to be valid';
  21.     }
  22.  
  23. sub process_data {
  24.  
  25.     my ($email_to_log) = @_;
  26.  
  27.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  28.     print $EMAIL_LIST $email_to_log;
  29.     close $EMAIL_LIST;
  30.  
  31.     print 'Go check your file to make sure it wrote correctly OR add some code to do it for you';    
  32. }
  33.  
  34. print $q->end_html();
  35.  
  36. 1;
Oct 15 '07 #5
eWish
971 Recognized Expert Contributor
Modification to the code I posted.
Change:
Expand|Select|Wrap|Line Numbers
  1. print $EMAIL_LIST $email_to_log;
To:
Expand|Select|Wrap|Line Numbers
  1. print $EMAIL_LIST $email_to_log, "\n";
Otherwise all of your email address' will end up on the same line. Also, the code I posted does not take into account duplicates.
Oct 15 '07 #6
eWish
971 Recognized Expert Contributor
Make sure you are using the proper path to perl or else you will get a 5OO error.
Widnow's will be something like C:\perl\bin\per l.exe

This line can be changed to your site's title, just change the wording "Mysite.com ".
Expand|Select|Wrap|Line Numbers
  1. print $q->start_html(-title =>'Mysite.com');
Oct 16 '07 #7
Mick1000
9 New Member
Hi,

Thanks again for your suggestion as you have been most helpful. I thought that in order to prevent any further pain for you :), I would post here my exact code and system set up as it currently stands to see whether you can identify any issues.

Setup

-Currently my server is Win2003 with IIS 6.0
-The 3 files email.pl, email.html and email.txt all reside in C:\cgi-bin
-My perl install is located in C:\perl\bin\per l.exe as you mentioned

My code is currently as follows:

email.html


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <FORM ACTION="/cgi-bin/email.pl" METHOD="POST">
  4. Email Address: <INPUT TYPE="text" NAME="email" size=30>
  5. <br><br><INPUT TYPE="submit" VALUE="Sign up">
  6. </FORM>
  7. </body>
  8. </html>
  9.  

email.pl


Expand|Select|Wrap|Line Numbers
  1. #! C:\perl\bin\perl.exe
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;
  8. use Email::Valid;
  9.  
  10. my $q = CGI->new;
  11.  
  12. print $q->header; 
  13. print $q->start_html(-title =>'mysite.com.au'); 
  14.  
  15. my $email_file = 'c:/cgi-bin/email.txt';
  16.  
  17.     if (Email::Valid->address($q->param('email'))) {
  18.        &process_data($q->param('email'));
  19.     } else {
  20.         print 'Your email address does not appear to be valid';
  21.     }
  22.  
  23. sub process_data {
  24.  
  25.     my ($email_to_log) = @_;
  26.  
  27.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  28.     print $EMAIL_LIST $email_to_log;
  29.     close $EMAIL_LIST;
  30.  
  31.     print 'Go check your file to make sure it wrote correctly OR add some code to do it for you';   
  32. }
  33.  
  34. print $q->end_html();
  35.  
  36. 1;
  37.  
-Also, to use Email::Valid, do I need to install/cfg anything further than the standard perl.

-For the IIS web extensions, is it advisable to use the .exe or .dll?

Cheers,

Mick
Oct 16 '07 #8
eWish
971 Recognized Expert Contributor
You will need to install Email::Valid which can be done using PPM. I use XAMPP which is an Apache Web Server environment (for development only) on a windows machine. For me the path to perl is as I showed you above. I am not certain how it is setup on Win2003 with IIS 6.0.

Do you still get error when you run the script? If so, then maybe this link can help you.

Kevin
Oct 17 '07 #9
Mick1000
9 New Member
i think im getting closer now. your "Go check your file to make sure it wrote correctly...... " line appears after i submit the email address. however still nothing written to file. when using the -w flag, i receive the following warning:

print() on closed filehandle $EMAIL_LIST

The warning indicates that the offending line of code is:

Expand|Select|Wrap|Line Numbers
  1. print $EMAIL_LIST $email_to_log;
  2.  
i have tried printing the html form input back to the screen which works correctly so it must be in the file write code??

thanks
mick
Oct 17 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

14
3058
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java functions defined which know how to read the hardware sensorts. I access it from wherever I am via any browser, and it displays the measurements (a set of simple numbers) as Java applets in a simple html page. One entry in this page (a table cell) looks...
6
3843
by: rxl124 | last post by:
someone please please help w/ this one. As I been working on this on and off and it just does not want to work. 1 #!/usr/bin/perl -w 2 3 $file = "/home/user1/dothis"; 4 open(FILE, ">$file"); 5 while($line = <FILE>) { 6 if ($line =~ /B/) {print FILE "A"}; 7 if ($line =~ /A/) {print FILE "B"};
8
6258
by: von | last post by:
I am writing data from a Javascript to a text file using a Perl script and it all works pretty well - except ... This: "Here is my data" becomes: "Here%20is%20my%20data" when it gets to the text file.
1
1734
by: von | last post by:
I am trying to write a single piece of data (that is generated from a Javascript) to a text file on my server via a Perl script. The Javascript is setup so that I can display the required data on my website using the following HTML: <span name='Data1' class='data1'></span> But I need it sent to a text file. :(
10
4472
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering the table definitions, all of which seems to work OK, I entered a tiny 8-row table and can do...
6
3358
by: rahulthathoo | last post by:
Hi I have my home directory on my departments server. Somehow I am not able to write to a file using a php code, $myFile = "trial3.txt"; $fh = fopen($myFile, "a+") or die("can't open file"); Instead of a+ if i use r or w or anything, it does not work. Could this be a permission thing? The entire directory had a 777 permission and even i after i created a file and chmod'ed 777 to it, the code couldnt open it. What could be the matter?...
21
34440
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
5
2326
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for explaining a "lamda" expression and so I don't know what one is and I am not doing any database stuff in the entire program. So what does this error message really mean and what can I do to get an On Error into the routine?
66
8199
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it didnt display the html after the perl script executed. Using perl 5.1 and apache 2.2.9 version(apache installed and run without any errors and no warning, perl tested fine) b) Also, when i clicked the html code to submit the upload of the...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.