Solved

Need a message board

Posted on 2000-04-10
12
303 Views
Last Modified: 2011-09-20
Just being lazy but does anyone have a cgi-perl script to display a text file and allow users to add to it via a form?  Save me learning Perl and writing it again.

Thanks,
Tim.
0
Comment
Question by:tims
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2700458
here is the code.

the only change you will have to make is to replace the path to perl with the path on your server (i.e. replace /usr/local/bin/perl with your path to PERL).

Also i am saving the messages in the /tmp directory in a file called mb.txt. you can change that as per your requirements.

==========message_board.pl
#!/usr/local/bin/perl

$|++;

use CGI;

$query=new CGI;

$text_file="/tmp/mb.txt";

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

if ($query->param){ ## message submitted by the user.
  $message=$query->param('message');
  $message=~ s/\s+/ /g; ##  Squeeze multiple spaces into a single space
  if ($message=~ /^$/ || $message=~ /^ $/){ ##  Empty message!! Not allowed
    print "<B><FONT COLOR=RED>You must enter some message</FONT></B><P>\n";
    exit;
  }else{
    ##  Open the text file in append mode
    open(MB,">>$text_file") || die $!;
    ##  Print an empty line & then write the users message
    print MB "\n";
    print MB $message,"\n";
    close(MB);
  }
}


##  Print the HTML form where the users enters the message
print qq{
  <FORM METHOD=POST ACTION=$ENV{SCRIPT_NAME}>
  <B>Enter your message here</B><BR>
  <TEXTAREA NAME="message" COLS="60" ROWS="10"></TEXTAREA>
  <BR><INPUT TYPE=SUBMIT VALUE="Save your message">
  </FORM>
  <HR>
};

##  show the content of the file
open(MB,$text_file) || die $!;
print "<pre>\n";
while(<MB>){
  ##  Show the messages in the file on screen
  print $_;
}
close(MB);
print "</pre>\n";
print "<P><HR>\n";
0
 

Author Comment

by:tims
ID: 2703289
Ta Maneshr,

It's just what I need but when I enter a string and click submit, it does not open the file.  I've had a quick play and the script stops on the
open(MB,">>$text_file") || die $!;
line.  I can populate the /tmp/mb.txt file by hand and it displays it ok first time.  I'm going to have more of a play later but if you know the answer......

Thanks,
Tim.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2703350
So what does the $! show?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tims
ID: 2703904
I've tried

print$!;
$!;
open ( MB, $text_file ) || print $!;
and
open ( MB, $text_file ) || $!;

none of them produce any different output.
Is that what you meant?
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2704106
tims,

here are a couple of things that you can try.

1) Change the path in $text_file from "/tmp/mb.txt"  to something else and then try.

2) when any CGI script is executed, it runs as a process under a given user.
(typically nobody on UNIX systems). what might be happening is this user may not have write access to the tmp directory. to confirm the same try this sample program.

=============t.pl
#!/usr/local/bin/perl

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

open(T,">>/www/web/cgi-bin/test.txt") || die $!;
print T "testing\n\n";
close(T);

print "Done\n";

try executing this script from your browser.

If you get the following message, that means the user on whose behalf  the CGI script is being executed does not have write permissions to that directory (/www/web/cgi-bin in this case).

The file access permissions do not allow the specified action. at /www/web/cgi-bin/t.pl line 6.

Now try the same script but replace /www/web/cgi-bin with /tmp.
Check the output message and you will know if permssion is indeed an issue here.
0
 

Author Comment

by:tims
ID: 2706859
I've tried

print$!;
$!;
open ( MB, $text_file ) || print $!;
and
open ( MB, $text_file ) || $!;

none of them produce any different output.
Is that what you meant?
0
 

Author Comment

by:tims
ID: 2706876
p.s. don't do a reload on this page... it inserts the same comment again.
I'll try the test today.  Ta.
0
 

Author Comment

by:tims
ID: 2707551
Yep, it was the permissions on the file.
Thanks maneshr, it's now working.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2707901
glad to hear that your problem was fixed :-)

Rgds
0
 

Author Comment

by:tims
ID: 2776526
Script is running happily.  Would you like to put "See above" or something to answer the querstion, maneshr?
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2777111
how about this??

i will just post my reply as an answer and you can go ahead and accept it.

Rgds
0
 
LVL 16

Accepted Solution

by:
maneshr earned 100 total points
ID: 2777114
here it is.

you can accept this as an answer.

thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 196
Port 80 requests 16 104
Excel to CSV conversion with specific columns 5 98
what are these modules in perl script 4 106
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question