[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need a message board

Posted on 2000-04-10
12
Medium Priority
?
309 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
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 85

Expert Comment

by:ozo
ID: 2703350
So what does the $! show?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 400 total points
ID: 2777114
here it is.

you can accept this as an answer.

thanks
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans
Suggested Courses

864 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