[thelist] php sessions

Andrew Forsberg andrew at thepander.co.nz
Tue Apr 23 01:25:01 CDT 2002


On Tue, 2002-04-23 at 10:05, John Corry wrote:

> On form 1, I:
> foreach ($HTTP_SESSION_VARS['ship_to'] as $key => $value)
> 			{
> 			$key = $value;
> 			$output .= "i did it!<br>\n";
> 			}
>
> What I'm doing here is assigning all the variables the values they were
> given when the form was submitted. Since all the form element's values are
> the same as their names:
> <input type="text" name="ship_to_name" value="<?= $ship_to_name?>">

Quick question: how do you get from $ship_to (an array) to
$ship_to_name? Also that foreach loop doesn't look like it's doing what
you want it to do (assigning the key for each session var to the value
of $value).

Rather than dump all $_POST data into a session it might be nicer to add
data to the $_SESSION array as it's validated. E.g.:

if (check_name($_POST['name']) == TRUE) {
    $_SESSION['name'] = $_POST['name'];
}

You may also want check_name() to return something more useful that true
or false (e.g.: always return $name['error'] = TRUE on any error, but
also return more specific info like: $name['error']['empty'] (no data);
$name['error']['numeric'] (not alphabetical, just numbers);
$name['error']['not_unique'] (there's already a user with that name);
etc etc etc) so the form can be 'smarter' -- it can inform the user what
it was they did wrong, and how they can fix it.

At any rate, once $_SESSION contains all the required fields the program
can switch to the second form and output data as easily as:

Your name: <?php echo ($_SESSION['name']); ?>

But, the short answer to your problem is that you probably want
$ship_to['name'] not $ship_to_name. The easiest way to check where $name
has gone is to use 'print_r($GLOBALS);' somewhere on the output page --
it's quick'n'nasty, but helps when midnight was a while back and your
brain feels too mushy. :-)

Cheers
Andrew





More information about the thelist mailing list