Solved

Need a message board

Posted on 2000-04-10
12
297 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 84

Expert Comment

by:ozo
ID: 2703350
So what does the $! show?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl rename 2 143
stftime format 4 55
iSeries PERL Scripts 7 157
Shell / Perl script to constantly comb a folder for new files & make copies of the new files 6 76
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 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