Hello PHPB!

I have a multi-page form designed to collect data from deployed soldiers. Their network connections are less than stable and I need a way for a user to start this form and as they traverse through it, their progress it kept somehow.

That way in the event of service disconnection, they can come back to item 17 of 18 and not item 1 of 18.

Is the answer PHP sessions and text files ?

Is the answer PHP session and array serialization perhaps?

Some combination of both?

Something entirely new to my PHP n00basaur brain perhaps?

What would be the logic and/or tools used to achieve this?

I hope this post finds you well and thank you in advance for any insight you may provide.

Respectfully,
Aaron

    If the user has to log in to access the form, then I might opt for a database solution as the most dependable solution. If no login is required, then I would pursue a session-based solution. With the session solution there should be no need for separate text files or array serialization that I can see: just let the sessions do their stuff. The main configuration issue will be ensuring that you set the session cookie and session data expiration limits to whatever you desire in order to keep the form data for as long as you deem necessary.

    But again, for more dependability and flexibility (such as what if the user signs in again from a different PC?), I'd consider requiring a user registration and login along with using a database to store their progress through the form.

      Thanks NogDog for your insight.

      Currently folks do not have to login or register but the data is getting databased once all the pages have been processed.

      Would you be willing to offer an overview of how one would implement Session saving and recalling?

      I mostly understand starting the session and looping through the values, but what I don't understand is where/how to keep the data so it can be looked up later when the user comes back.

      Is it new table?
      What fields should I create/use to ensure the user is able to get their progress back?

      Respectfully,
      Aaron

        At its most basic, when the user submits data from a form page, you can simply copy the $POST data into the $SESSION array:

        <?php
        session_start();
        foreach($_POST as $key => $data)
        {
           $_SESSION[$key] = $data;
        }
        

        This is, of course, rather quick-and-dirty and perhaps problematic in terms of reliability/security. A better choice might be to create sub-arrays within the main $SESSION array to store the data from each form page. So for example, in the code that accepts the form submission from page 3 of the form sequence:

        <?php
        session_start();
        $_SESSION['form_data'][3] = $_POST;
        

        Then at any point in the process where you wanted to access some field from the form page 3 data (let's say there was a field named 'foobar'):

        <?php
        session_start();
        
        $foobar = (isset($_SESSION['form_data'][3]['foobar'])) ? $_SESSION['form_data'][3]['foobar'] : null;
        

          Hmm I'm trying to understand it, but I don't see the code above making the data persistent over a period of time.

          Say I just landed on page 3 and then my internet connection gets disrupted for 24 hours.

          What would be an efficient method of retrieving those first two pages of answers from this user from 24 hours ago and then continuing the form?

          V/R,
          Aaron

            Thinking about it more, one would have to have a user based system in order to keep any form data exclusive to said person right? With the possibility of multiple users on one host I don't see how to avoid it.

              Sessions, by definition, persist over some period of time. The session data specific to a given user is identified/accessed based on the unique session ID, which in normal PHP usage is communicated between the client (user's browser) and the server via a cookie. How long that data persists is dependent on the specified lifetime for that session ID cookie (the default PHP setting is normally "0", which means "until the browser is closed) and how long the data will remain on the server before the session "garbage collector" will delete it, which is based on a number of configuration settings (see the session.gc_* configuration items on this manual page).

              Where this might possibly be problematic in your case is if you think it possible/likely that different users might use the same computer and browser and/or a single user might want to use different computer/browser during this sequence, as in the first case the multiple users would end up sharing the same session, while in the latter case when the user moved to a second computer he would not have the session cookie so would start at the beginning again. If either of these cases is something you think you need to handle, then using a login system with form data stored in a database and related to the login ID would probably be a better approach.

                Write a Reply...