in reply to Beginner CGI programming, authentication

I'm in the "keep it simple, stupid" camp. I'd have the normal URL invoke a CGI script to show the data, like
/path/to/the/message
but when your friend wants to edit it, adds ?mode=edit to the end of the URL:
/path/to/the/message?mode=edit
which invokes the same program, but the param is detected, and now a form comes out with two fields: the original text in a textarea, and a place for a password. When this form is submitted, it'll be returned to the same CGI program, which can check the password against a crypt() password included in the program, and if a match, updates the file and shows the new result. Code looks something like this:
use CGI qw(:all); my $MESSAGE_LOCATION = "/path/to/file.txt"; my $CRYPTED_PASSWORD = "aaPwJ9XL9Y99E"; ## this is from print crypt("hello", "aa"), so "hello" is the password print header, start_html; if (my $new_text = param('text') and my $password = param('password')) + { ## update mode... is the password good? if (crypt($password, $CRYPTED_PASSWORD) eq $CRYPTED_PASSWORD) { open F, ">$MESSAGE_LOCATION" or die; print F $new_text; close F; print h1('updated!'), p('The message was updated!'); } } elsif (param('mode') eq 'edit') { ## secret mode open F, $MESSAGE_LOCATION or die; my $message = join '', <F>; close F; print h1('edit the message'); print start_form; print textarea('text', $message); print 'password: ', password_field('password'); print submit('update'); print end_form; } ## always display the result: open F, $MESSAGE_LOCATION or die; my $message = join '', <F>; close F; print h1('Welcome to our info page'); print h2("Today's top tip:"), escapeHTML($message); print end_html;
There. Whipped out quickly, so it might not fully work, but you can flesh out the details I hope.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: &bull;Re: Beginner CGI programming, authentication
by mkahn (Beadle) on May 16, 2003 at 01:11 UTC
    Thanks for the responses, randal and katie.

    This works fine, but the permission on the text file needs to be set to 777 to work in a browser.

    HTAccess is probably a more accepted way of doing this, but I like avoiding the pop-up.

      Nothing should ever be "777".

      It's true that the userid of the webserver needs to be able to read and write the message file, but there are (at least) two ways to accomplish that:

      • Use the chown command to make the file owned by that user.
      • chmod the file to 666 (not 777)
      You never want to have a world writable file also be an executable. That's just begging for someone to come along and put random content into it and then executing that.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Good point. My client's server writes to the text file with a 6-- code, while my test server requires --6 permissions.