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

CGI.pm permits one to save the current state of a form using code similar to the following:
unless (open (Q_NEW_USER, ">/home/web/new_user.dat")) { print "Unable to open file for data storage."; } else { $q->save(\*Q_NEW_USER); }
Later, in a different form handler, one can retrieve the previously stored values as follows:
unless (open (Q_NEW_USER, "/home/web/new_user.dat")){ my $message = "unable to open data file."; $q_new_user = new CGI; } else { $q_new_user = new CGI (\*Q_NEW_USER); }
The above code works great as long as only one user is accessing the form at a time. But if two users access the form concurrently then the second user can see the first user's data.

Clearly, the problem is that every query object is stored using the same filename.

What would be the best way to provide for access by multiple concurrent users? I might mention that the site uses an Apache .htpasswd file to restrict access so that every user has his own unique login to the Apache 1.3 webserver.

Replies are listed 'Best First'.
Re: Managing Concurrent CGI.pm Query Objects
by mr.nick (Chaplain) on Mar 28, 2001 at 06:42 UTC
    I'd suggest (if you can) using Apache's UNIQUE_ID environment variable:
    my $unique_filename="/home/web/user-".$ENV{UNIQUE_ID};
    This variable will remain the same throughout an entire session and unique for each user.
Re: Managing Concurrent CGI.pm Query Objects
by arturo (Vicar) on Mar 28, 2001 at 06:43 UTC

    It seems to me you have the biggest piece of the puzzle already. One way is to have a way of uniquely identifying each user; and to incorporate that unique identifier into the name of the file you save away. Since your users are authenticated before they ever get to see any content in the directory your CGI lives in, you *can* get the user's name at the time you generate the file in the first place, so this shouldn't be at all difficult.

    If it's a function to get that name that you're looking for, CGI::remote_user()

    My apologies if I'm missing something here.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Managing Concurrent CGI.pm Query Objects
by clintp (Curate) on Mar 28, 2001 at 06:37 UTC
    What you'll want to do is use a filename that's generated uniquely for each user. There are lots of ways for generating a unique key and we don't need to go into that here.

    So generate the key, save the data in a filename that's constructed from the key and pass the key to the user. Use a cookie, or a hidden form element, or something.

    Later, when you need to get the data back, use the key that's hidden in the form or from the cookie.

Re: Managing Concurrent CGI.pm Query Objects
by damian1301 (Curate) on Mar 28, 2001 at 06:33 UTC