Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm doing a university group project and we're using Perl (which we've only been learning for three odd weeks), and the deadline is VERY close, so if anyone can help us out it'd be great.
These might seem like dumb questions, but the O'Reilly books and all the other sites I've looked at were no help, and I'd rather be proven dumb than fail my course ...

We're programming a questionnaire system, and we've got a login script which verifies a user's identity. This then calls a script which creates the questionnaire, which passes the answers to a final validation script. We have to get the user identity from the login script to the validation script (so that the user can be marked as having filled in the questionnaire, but only once their answers have been validated), but can't just tag it on the end of the URL as plain text when passing it between scripts, cos we are required to keep the user identity secure. We've thought of using cookies, but is there another (simpler) way of doing it?

Also, in the questionnaire script, the questions are named 'questionN' where 'N' is the question number, but in the validation script we need them to be just 'N' ... anyone know how to strip the string 'question' away from the number? :-)
  • Comment on Urgent help needed by inexperienced programmers

Replies are listed 'Best First'.
Re: Urgent help needed by inexperienced programmers
by PsychoSpunk (Hermit) on Dec 07, 2000 at 02:40 UTC
    My gut instinct is to store the user identity in a file associated with a number. When the questionnaire has been validated, delete the line that contains the translation. This of course requires that the adminstrator of the script is scrupulous and does not view the file that associates number to user. This is a simple way to handle it, and may or may not be feasible in a RL situation, but you've already said it's for a class, so in lieu of cookies, that's another option. This is known as storing persistent data, and while it's not exactly the cookbook way, it's under the same idea. (ie - read this to mean, go reread the Perl Cookbook aka the Ram)

    As for your questionN -> N. You could duplicate the data from CGI.pm and set it up so that:

    Update: The code here is flawed, see Re: Re: Re: Urgent help needed by inexperienced programmers to understand why.

    @params = $q->param(); # assuming using OO style of CGI.pm foreach $val @params { if ($val =~ /^question(.+)/) { $$1 = $q->param($val); } }

    and now they are accesible via just N, well $N to be fair.

    Stick around, and you may see better answers. Remember this is just what first came to my mind.

    ALL HAIL BRAK!!!

      The external file sounds like a good idea, you'd need to keep the ID number in each of the pages so you can match it up w/ the user at the end ... not secure, of course. I can put a number in my hacked copy of your script too.

      There's been lots of discussion on why $$1 = ... is a *bad* idea; generally it means you're looking at the wrong end of the problem. You could do:

      foreach $val @params { if ($val =~ /^question(\d+)/) { $questions[$1] = $q->param($val); } }
      which'd give you the question vals in an array, indexed by N but ... that's only slightly better. Overwrites if N is in there twice, etc.

      a

        I'd never do it that way. I was just providing a method that would meet spec. There's not even a reason to do what you're suggesting since it's duplication of data (ie - memory issues). But, some caveats to your code. We don't know if N will be integer. We presume it will be, but what about cases 1a, 1b, 1c?

        Furthermore, my code has that problem in the case where $1 = 1. in which case you're evaluating ${$1} -> $1 -> 1;

        My answer was to generally push these guys in the direction proper, and your reply shows why my initial thought of stripping question from N is bad. I hadn't seen that before a, thanks.

        It appears that a lot of people use the ID number, since it appeared on another post as magic cookie. I left a lot of my answer open ended since this is a group project for school, and I think they should be forced to use their heads a little. You are correct about the ID being stored on the questionnaire page, but I figured that it was obvious.

        ALL HAIL BRAK!!!

Re: Urgent help needed by inexperienced programmers
by rlk (Pilgrim) on Dec 07, 2000 at 04:58 UTC
    ...can't just tag it on the end of the URL as plain text when passing it between scripts, cos we are required to keep the user identity secure.

    I don't know how specific your assignment is, but my suggestion would be to do something like this, if you can:

    (Mostly static page w/ form for user name and password, and code to display an error message if one is specified.)
    -->
    (Script which tests user/pass, and either redirects to first page with login error, or displays questionaire form, with user/pass as hidden vars.)
    -->
    (Script which again tests user/pass, redirecting to page one on error, verifies validity of questionaire answers, redirecting to page 2 on error, and (if all is well) logs answers, etc.)

    Security Issues:
    1) Serve this with SSL if possible. Otherwise you're transmitting password data in clear text over the network. If you don't have access to a secure server, this method is no worse than any other (short of a java app implementing PKI, which would probably be a bit overboard ;)

    2) Pages containing passwords may remain in browser cache. There are various ways to solve this. The easiest would probably be to have the page which recieves the form submission from the login page set a temporary cookie containing the user/pass data, instead of passing it around in hidden form variables. The validation page would then simply test for an appropriate user/pass from the cookie, instead of from the form.

    Another option is the use of a "magic cookie" number. (NOT the same thing as a browser cookie, don't get confused). A "magic cookie" is a random number (long enough to be unguessable) that the server generates when it validates your login. The number is then tied to your identity in a file (or DB, or some more esoteric method of storing data) on the server. The magic cookie is then passed back to the browser in the hidden form variables, instead of the user/pass. The validation script gets the magic cookie, and compares it to the file to find the user. The advantage of this method is magic cookies can be timed out, so If I steal the number from your browser's cache an hour or two after you've used it, it's no longer valid, and I can't impersonate you.

    --
    Ryan Koppenhaver, Aspiring Perl Hacker
    "I ask for so little. Just fear me, love me, do as I say and I will be your slave."