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

I am writing CGIs most of which contain forms and am using the POST method. I need to know the id of the user as they navigate around the site. The solution I have come up with is to have an hidden input field in the forms with the id value. This is rather cumbersome as it makes the id difficult to access. Is there another more elegant way of doing things. Maybe through setting some environment variable? Thanks.
  • Comment on CGI question: carrying information from CGI to CGI

Replies are listed 'Best First'.
Re: CGI question: carrying information from CGI to CGI
by chromatic (Archbishop) on Feb 23, 2000 at 21:21 UTC
    If you're using CGI.pm, it's pretty easy to get at any param. Suppose you set a hidden field named ID with the id value:

    <input type="hidden" name="id" value="$id"> Then, just get at it with:

    my $id = $q->param("id") || new_id();

    If you're not using CGI.pm, you probably should be.

Re: CGI question: carrying information from CGI to CGI
by btrott (Parson) on Feb 23, 2000 at 23:07 UTC
    You could investigate using cookies instead of hidden form fields.

    Cookies are quite simple w/ CGI.pm. To set a cookie:

    use CGI; my $query = new CGI; my $cookie = $query->cookie(-name => 'id', -value => 'foo'); print $query->header(-cookie => $cookie);
    To retrieve a cookie:
    use CGI; my $query = new CGI; my $id = $query->cookie(-name => 'id');
    There are other parameters you can set for a cookie--read the CGI.pm docs to find out about them.
RE: CGI question: carrying information from CGI to CGI
by dlc (Acolyte) on Feb 24, 2000 at 00:08 UTC
    Environment variables are no good in this case, because they don't outlive the process in which they were created. A combination of cookies (session-ids) and database-based sessions is my preferred method of operation.
Re: CGI question: carrying information from CGI to CGI
by Anonymous Monk on Feb 23, 2000 at 23:02 UTC
    You might want to check out "Cookies" by Simon St. Laurent.