As a journeyman Perl programmer i'm rather experienced in CGI applications and sessions. I've only recently began exploring PHP. As an experiment, I'm converting an existing Perl webapp to PHP and have yet to find a way to register all post/get variables to the session with one method. In Perl I could simply associate the CGI object with the session object and bam!, all CGI vars are saved in the session. Is there a native PHP method to do this that I haven't found? I currently use MySql to store my Perl CGI::Application session information. I would like to emulate the simplicity of this behavior in PHP. Any hints?

Thanks

    //...session initialization stuff here
    
    foreach($_REQUEST as $key => $value) {
      $_SESSION[$key] = $value;
    }
    

    thats one method...
    $REQUEST is a combination of get and post vars. i know you know about foreach coming from perl, that just takes the array keys and values from the get/post and stores them in the session global with the same keys values.
    $
    POST['something'] becomes $_SESSION['something'] after the loop.

      Hi pm,

      If I understand you correctly ...

      $_SESSION['REQUEST'] = $_REQUEST;

      Paul 😉

        Originally posted by paulnaj
        Hi pm,

        If I understand you correctly ...

        $_SESSION['REQUEST'] = $_REQUEST;

        Paul 😉 [/B]

        that kinda works but you end up with an array like
        $_SESSION['REQUEST']['gp_key'] = "value";

          Thanks, guys. I figured I could simply loop through the $_REQUEST array and assign the vars to the session that way. I was thinking something like this:

          $session->save_param($cgi, $self->param("field_names"));

          where the $session->save_param object method takes the $cgi object as an arg and also takes the "field_names" array as an optional argument. The method then saves the cgi params in the field_names list to the session.

          Your solution looks quite simple. It's sometimes difficult to stop thinking "Perlish".

          Thanks again.

            If you are using PHP v5+ why not use array_merge.

            $_SESSION= array_merge($_SESSION, $_REQUEST)
            
            //or 
            
            $_SESSION= array_merge($_SESSION, $_GET, $_POST)
            
            

            I guess the only factor here is that it would help if they are associative arrays

              Write a Reply...