On Wed, Jul 08, 2009 at 06:55:24PM -0500, Shawn McKenzie wrote: > D.M.Jackson wrote: > > Thanks guys. I was just wondering if it was common practice to pass all > > those variables in the SESSION object or if I was following a bad example > > because it was the first time that I had seen so many variables passed this > > way. If this is the typical way of handling this in php then I don't > have a > > problem with it, I just wanted to make sure that I wasn't getting off to a > > bad start and picking up bad habits while learning php. > > > > Thanks, > > Mark > > Again, it depends upon whether you "need" those variables in the next > page for example. Think of a wizard, where you fill some values in a > form, click next, fill more values, click next, etc and then click > finish. You may want to pass the values from each page to the next via > the session and ultimately have them all available in the last page. > There are others ways to do this, such as adding them as hidden inputs > in the next pages, but I personally would use sessions. > > One other example might be user info, id, username, firstname, lastname, > current access role etc. You may use these on every page, so once you > retrieve them from the db, you can store them in the session. Any other > info like email, age, register date whatever, you can retrieve only when > needed.
Just to provide a counterpoint to this, I would discourage using $_SESSION for more than absolutely necessary. If I have a situation such as Shawn mentions above, I pass values via hidden fields in the form. Most of the forms I create are backed by a database, so mostly I capture data from there. In addition, you can serialize data you wish to save and store it in a database or hidden field, and then unserialize it upon painting the next page. If I'm not mistaken, there's a limit to the data which can be stored in a session variable. I don't want to mistakenly hit that limit and wonder what happened. And besides, I just think $_SESSION should be reserved for *special* cases. And, as mentioned before, it's worthwhile asking yourself if you *really* need to remember a bunch of information from page to page. The need to do so may well be a result of lazy programming habits. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php