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

Before I travel down the path of using session variables, is it possible to store a variable as a session param from one page and access it again from a different page? I'm assuming I would have to forward the session ID to the target URL with GET or POST. I was passing a simple hidden form value for a customer name using GET and a CGI Redirect. Then I parsed the QUERY_STRING to get the customer name and stored it to a variable. However, when I reload the page, that variable changes because the QUERY_STRING changes. So I'm looking to store the customer name to a session table in MySQL and reference it from the second page. So again, do I just pass the session ID via GET or POST and store it as a variable on the target page?

Regards,
Scott

First page passes the customer name to the second page:
$redirURL = "http://localhost/cgi-bin/add_building_names.cgi?customer= +$post_new_customer"; print $q->redirect(-URL => $redirURL);

The second page picks up this variable and stores it:
my @values = split(/=/,$ENV{QUERY_STRING}); my $customer_get = $values[1];

I then had to use hidden fields and POST to allow this name to persist across reloads. I think storing it as a session variable and/or local cookie would be better. I'm just trying to understand how to reference that variable from the second page.

Replies are listed 'Best First'.
Re: Understanding the Use of Session Variables
by moritz (Cardinal) on Mar 05, 2009 at 16:09 UTC
    To pass session variables between different pages you normally use cookies. CGI::Session::Tutorial should answer most of your questions.

    Oh, and use CGI::Session and CGI instead of hand-rolling your own CGI parser (looking at that nasty split line...)

      Thanks for the reply. I am already using CGI (from my code above I didn't show you that $q-> new CGI();) and I am looking to use CGI::Session b/c I agree that splitting the QUERY_STRING is not the best method. But then again, we all crawled before we ran, right? I'm not a programmer by any means. It is simply by the virtue of the helpful folks here at PerlMonks and countless hours of reading/writing/testing that I am able to perform the minimal Perl/CGI writing that I do. Once I set a cookie, how to I access it from the second page? I'm assuming that if I use the default method for generating a cookie that it's name will be the session ID. So I still have to know what that is to reference the cookie from page two, right?

      my $session = new CGI::Session(); print $session->header();
        Did you even read the tutorial I linked to? In the end you have to understand what you're doing, and the best way is to read introduction level tutorial like the one I gave you, and then start experimenting.
        Once I set a cookie, how to I access it from the second page?

        By using CGI:Session. It looks in the header, finds the session ID in the cookie, and then loads the session object from local storage.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Understanding the Use of Session Variables
by locked_user sundialsvc4 (Abbot) on Mar 05, 2009 at 17:04 UTC

    Conceptually, “session variables” are a pool of persistent information that is stored on the host, in such a way that they can be quickly retrieved and updated with each incoming request.

    The session information is located using some kind of random identifier, customarily stored as a “cookie,” or as a hidden form-field, or as a part of the URL information, or some combination of the foregoing.

    Customarily, these mechanisms are used only to transport a random identifier, which is not ”trusted” but rather is subjected to rigorous validation. The client-side is never trusted as a source of original information.

    As you peruse the various session-handling modules on CPAN, you will observe how they typically separate the various concerns ... session identification, the transport of the session-identifier between client and host, and session storage. This will give you the flexibility to configure exactly what is needed for your particular application, and to change it in the future if necessary.

    I, too, would judge that you have not yet taken the time to avail yourself of the introductory materials to which you, have at several times, thus far been pointed . . . Take care not to waste our time, nor your own!

      Thanks everyone for the replies. While it is obvious to me I have MORE research to do b/c I don't yet understand all of the pieces, what I also don't understand is the attitude from some of you that I am wasting your time:

      I, too, would judge that you have not yet taken the time to avail yourself of the introductory materials to which you, have at several times, thus far been pointed . . . Take care not to waste our time, nor your own!

      I am NOT a programmer. I am a Systems Engineer and design networks. If some of you can read the tutorials and understand it the first time around, good for you. But that does not give you the right to assume that I am here just to pass the work off to someone else to figure things out for me. I HAVE read several different tutorials, and the fact that I STILL need help doesn't mean you should assume I haven't tried anything on my own. I have figured out many of my programming issues on my own, and have come to the conclusion that cookies should work better for me after completely reworking my code to support first GET and then POST. For those of you that are willing to help others work through their problems, post a reply. For those of you that want to make a negative comment regarding the fact that I have read a lot on the subject and still don't understand it, keep your replies to yourselves.
        Sorry spickles, but you can't control other people, its best not to fret over that.

        Instead of split, use CGI's url_param, see MIXING_POST_AND_URL_PARAMETERS

Re: Understanding the Use of Session Variables
by ikegami (Patriarch) on Mar 05, 2009 at 16:36 UTC

    You say "session", but you're using a parameter. Using a parameter is much simpler and will do the trick just fine. The only catch is that it's subject to manipulation by the user. If that's not a problem, the second page would do:

    my $customer_get = $q->param('customer');