G'day all
I have been trying to write a script which takes a user's input from a form on the submission page (addnews.html) and adds it to the main page (index.html).
The plan is to store the addnews.html file in a .htaccess protected directory and have a static un-updatable password to stop everyone and everbody adding news.
My script currently is:
#!/usr/bin/perl
# news.pl
# Attempt #3 at a news script.
# Set up the standard HTML header.
print "Content-type: text/html\n";
# Read in the data from the "addnews.html" file.
$data_length = $ENV{'CONTENT_LENGTH'};
$bytes_read = read (STDIN, $temp_data, $data_length);
# Store the 'temp_data' in an array called '@temp_name_value' for mani
+pulation.
@temp_name_value = split (/&/, $temp_data);
foreach $temp_name_value (@temp_name_value)
{
# Split up the 'name=value' pairs.
($name, $value) = split (/=/, $temp_name_value);
# Replace '+'s with ' 's.
$name =~ tr/+/ /;
$value =~ tr/+/ /;
# Translate hexidecimal characters back into their original charac
+ters.
$name =~ s/%(..)/pack("C",hex($1))/eg;
$value =~ s/%(..)/pack("C",hex($1))/eg;
# Create an associative array using the '$name" and '$value' varia
+bles.
# NOTE: If there is no '$name' specified '$value' is used instead.
if ($form_data{$name})
{
$form_data{$name} .= "\t$value";
}
else
{
$form_data{$name} = $value;
}
}
# Create a variable to store the location of the display page, "index.
+html".
$news_data = "/index.html";
# Open the '$news_data' file to read the current data or die with an e
+rror message.
open (NEWS, ">$news_data") || die "Unable to locate $news_data, th
+e news data file.";
# Read the data form '$news_data' into an array called '@news'.
@news = <NEWS>;
# Close the '$news_data' file after use.
close (NEWS);
# Re-open the '$news_data' file for the input of the new data.
open (NEWS, "<$news_data") || die "Unable to locate $news_data, th
+e news data file a second time.";
# Write the data back into the '$news_data' file with the new data inc
+luded where appropriate.
# Loop through each line of the '$news_data' file and write it back.
foreach $line (@news)
{
# Search for the '<!--latest_news-->' marker.
if ($line =~ /<!--latest_news-->/i)
{
# Write itself back for future use.
print NEWS "<!--latest_news-->\n";
# Add the 'nickname' of the poster.
print NEWS "From: $form_data{'nickname'}</TD>\n<TD>";
# Add the 'headline' of the post.
print NEWS "<B>$form_data{'headline'}</B></TD>\n<TD>";
# Add the 'news_text' underneath the 'headline'.
print NEWS "$form_data{'news_text'}";
}
else
{
die "Unable to find the position header in $news_data, the
+ news data file.";
}
}
# Close the '$news_data' file after use.
close (NEWS);
# Provide the poster with a link to their news item.
print << "EOF";
<H2 ALIGN="center">Thank you for your news item</H2>
<P ALIGN="center">Click <A HREF="../index.html">here</A> to view y
+our news item.<BR>
Note that you may have to reload the file before your post is visi
+ble.</P>
EOF
# EOF news.pl
Could someone please give me some ideas on where to go from here, in terms of making it functional..?
Thanks
David
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.