RE: [PHP] An easier way? $_POST[] => $_SESSION[]

From: Date: Mon, 01 Nov 2004 18:00:26 +0000
Subject: RE: [PHP] An easier way? $_POST[] => $_SESSION[]
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-200851@lists.php.net to get a copy of this message
Erich Kolb wrote: > Is there an easier way to assign all post data from a form to session > data? > > Eg., > > $_SESSION['first_name'] = $_POST['first_name']; > $_SESSION['last_name'] = $_POST['last_name']; > > $_SESSION['email'] = $_POST['email']; You could do this: $_SESSION = array_merge($_SESSION, $_POST); Although it might be better to ensure that only known valid keys from $_POST are making their way into $_SESSION, doing something like this: $keys = array('first_name', 'last_name', 'email'); foreach ($keys as $key) { if (isset($_POST[$key])) { $_SESSION[$key] = $_POST[$key]; } }

« previous php.general (#200851) next »