in reply to Re^3: HTML Template
in thread HTML Template

Many thanks :)

I don't know whether I'm doing it correctly. I've a template page named form.tt. On this template, I've a template variable "key" (<TMPL_VAR key>). When a user A requests the page, the template variable is filled with a random MD5 hash. For illustration, here's the tear-down code:
use CGI; our $q = new CGI; sub do_form { my $href; $href->{key} = md5_hex(time() . '-' . $$ . '-' . rand()); to_template( output => $href, tmpl => 'form.tt' ); } sub to_template { my %hash = @_; print $q->header; require Template; my $template = HTML::Template -> new(filename => "/templates/$hash +{tmpl}"); $template->param(map { defined $hash{output}->{$_} ? ($_ => $hash{ +output}->{$_}) : () } keys %{$hash{output}}); print $template->output; } sub process_form { my $params = $q->Vars; debug($params->{key}); }
Assume users A and B access the form at the same time, both should get a different key set on their respective form. Am I right?

Now A and B submit the form at the same instant, does the variable $params->{key} hold the right value of each user? Is it possible for user B's submission to get the value of A's key?

Replies are listed 'Best First'.
Re^5: HTML Template
by punkish (Priest) on Jun 14, 2010 at 03:16 UTC
    Now A and B submit the form at the same instant, does the variable $params->{key} hold the right value of each user? Is it possible for user B's submission to get the value of A's key?

    No. Since you are not getting the value of $params->{key} from the memory of the server on which your application is running, but instead, your user is passing the value back to the server, every user is passing a unique, user-specific value. In other words... your script.cgi creates a random value of the key, sends it to user A in a form and user A sends that value back to script. Similarly, the script.cgi creates another random value of the key, sends it to the user B, who sends that value back to the script. Both users get different values. There is no clash whatsoever.

    Read up on sessions, and everything will be clear.

    --

    when small people start casting long shadows, it is time to go to bed
      Does it matter that the variable $q (a CGI object) is declared with "our" rather than "my"? This variable is exported and used in other modules.
        You are seriously mixing apples and oranges. As long as you are not dipping into the server's memory (for example, via mod_perl or some other persistence mechanism), it doesn't matter whether you are declaring a variable with my or our or their or her or his. It doesn't even matter whether you are using Perl or Ruby or Python or whatever.

        Think of it this way... when user A sends requests a web page, a custom web page is constructed for user A, and sent back to user A. No one, no one in the world, in fact, not even user A using another browser on the same computer, gets that first page. Only user A, within the browser from which user A requested the page, only that user gets that page. That connection, between that instance of the browser and the web server, that connection is a session, and that session lasts only for the duration of that connection. To make it last longer... well, that is session and state management, and that is your responsibility.

        Of course, if you use any persistence framework, all bets are off, and session management kicks in. Once again, you will be greatly helped by reading up on sessions and state management in web processes.

        --

        when small people start casting long shadows, it is time to go to bed