Solved

Need a message board

Posted on 2000-04-10
12
294 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
  • 6
  • 5
12 Comments
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
Comment Utility
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
Comment Utility
So what does the $! show?
0
 

Author Comment

by:tims
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:tims
Comment Utility
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
Comment Utility
Yep, it was the permissions on the file.
Thanks maneshr, it's now working.
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
glad to hear that your problem was fixed :-)

Rgds
0
 

Author Comment

by:tims
Comment Utility
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
Comment Utility
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
Comment Utility
here it is.

you can accept this as an answer.

thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now