Need a message board

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.
timsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
maneshrConnect With a Mentor Commented:
here it is.

you can accept this as an answer.

thanks
0
 
maneshrCommented:
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
 
timsAuthor Commented:
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
ozoCommented:
So what does the $! show?
0
 
timsAuthor Commented:
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
 
maneshrCommented:
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
 
timsAuthor Commented:
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
 
timsAuthor Commented:
p.s. don't do a reload on this page... it inserts the same comment again.
I'll try the test today.  Ta.
0
 
timsAuthor Commented:
Yep, it was the permissions on the file.
Thanks maneshr, it's now working.
0
 
maneshrCommented:
glad to hear that your problem was fixed :-)

Rgds
0
 
timsAuthor Commented:
Script is running happily.  Would you like to put "See above" or something to answer the querstion, maneshr?
0
 
maneshrCommented:
how about this??

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

Rgds
0
All Courses

From novice to tech pro — start learning today.