Jump to content

[SOLVED] Retaining Singleton after Form Submit


theobserver

Recommended Posts

Hello.  I have written some code to handle some form processing.  Essentially, everything is in one script (and a subordinate class) that displays a form, then based on a button click submits back to the original script to perform some other action (the forms and page displays are contained in functions in the class, and the script takes a passed or POSTed value and uses that to determine what to show and what processing needs to be done on POSTs returned).

 

I have need for a Singleton to retain some login information and other data I need to keep between page submits (the form relies on a database lookup to populate some of the fields, and rather than try to pass around stuff in the form via hidden fields, I'd rather be able to look it back up as needed from the Singleton).  I've tried several different ways to make the Singleton and it works fine when the script first loads, but after I submit the form, it disappears, and as such is not doing what I needed it to do.

 

Does anybody have any suggestions on how to maintain a Singleton between form submits?  Perhaps a redesign is in order? 

 

Thanks for your time.

 

Link to comment
Share on other sites

Does anybody have any suggestions on how to maintain a Singleton between form submits?

 

Place it in the $_SESSION array, then grab it back out after the request. Sessions are automatically serialized so as long as the class defianition is still available you'll be fine.

Link to comment
Share on other sites

I used the $_SESSION array as you indicated, and placed a session_start()

on the login page that precedes the page/script I indicated in my first post.  However,

after I submit the page back to itself, the $_SESSION array is empty.

 

Any suggestions on what may be happening?

 

Thanks again.

Link to comment
Share on other sites

Can we see?

 

A simple example.

 

<?php

  session_start();

  class foo {
    private $n;
    function __construct($name) {
      $this->n = $name;
    }
    function hello() {
      echo "hello " . $this->n;
    }
  }

  if (!isset($_GET['clicked'])) {
    $foo = new foo('thorpe');
    $_SESSION['foo'] = $foo;
    echo "<a href=\"?clicked\">click to say hello</a>";
  } else {
    $foo = $_SESSION['foo'];
    $foo->hello();
  }

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.