NYCPHP Meetup

NYPHP.org

[nycphp-talk] handling forms (relative newbie)

Adam Maccabee Trachtenberg adam at trachtenberg.com
Thu Oct 2 18:26:47 EDT 2003


My preferred way of handling this is something like this:

$fields = array('username', 'password', 'zipcode');

foreach ($fields as $f) {
    $_SESSION[$f] = $_POST[$f];
}

This avoids:

1) The $_SESSION clobbering problem.
2) Needing to do $_SESSION['_POST']['username'].

It does require you to update the array when the fields change, but
this way you're guaranteed to only get the fields you want. I like the
array_merge() solution if you want all the variables.

-adam

On Thu, 2 Oct 2003, Scott Mattocks wrote:

> If you used $_SESSION['_POST'] = $_POST then the session array has as an 
> element an array containing the post data.  Since you have an array in 
> an array you would access it like this:
> echo $_SESSION['_POST']['first_name'];
> 
> Scott Mattocks
> 
> Aaron Fischer wrote:
> 
> > That works, thanks!
> > 
> > One thing I am wondering is why one needs to use the line:
> > $_POST = $_SESSION['_POST'];
> > 
> > If $_SESSION now has the array stored, it seems that this should work:
> > echo $_SESSION['first_name'];
> > 
> > ?
> > 
> > -Aaron
> > 
> > On Thursday, October 2, 2003, at 03:26 pm, David Rydell wrote:
> > 
> >> session_start();
> >> session_register('_POST');
> >> $_SESSION['_POST'] = $_POST;
> >> header ("Location: session_results.php");
> >> exit;
> >>
> >> Session Results page:
> >>
> >> session_start();
> >> $_POST = $_SESSION['_POST'];
> >>
> >> echo $_POST['first_name'];
> >> echo $_POST['last_name'];
> > 
> > 
> > _______________________________________________
> > talk mailing list
> > talk at lists.nyphp.org
> > http://lists.nyphp.org/mailman/listinfo/talk
> > 
> > 
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
> 

-- 
adam at trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!





More information about the talk mailing list