theobserver Posted July 19, 2007 Share Posted July 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted July 19, 2007 Share Posted July 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
theobserver Posted July 19, 2007 Author Share Posted July 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted July 19, 2007 Share Posted July 19, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
theobserver Posted July 19, 2007 Author Share Posted July 19, 2007 Looks like I had another issue that was causing the problem. I fixed that and it works fine... Thanks again for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.