Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
A small problem that has me kinda baffled.  This is the situation:

I've set up a self-submitting form FORM.PHP.  Once the inputs are
validated, the info gets put in SESSION variables and the script
redirects the user to a review form REVIEW.PHP which displays the info
the user has input and allows them to edit or submit.  At the top of
that page, I have the following code:

--- start code ---

foreach ($_SESSION as $key=>$submission) {
if ( empty($submission) || ($submission == "") || (strlen($submission)
== 0) ) {
$DISPLAY[$key] = "« BLANK »";
else {
$DISPLAY[$key] = $submission;


--- end code ---

The form then displays the info now extracted from the $DISPLAY array.

By creating a new array $DISPLAY for the $_SESSION variables, the idea
is that this page will indicate the optional fields where the user
didn't input data with the string, "BLANK", without changing data in
the $_SESSION array.  At the bottom, there is an edit button which will
take a user back to the original form where the form will be
repopulated with the $_SESSION data.

The problem I'm having is that the blank $_SESSION variables are being
overwritten with the "BLANK" string that's only meant to appear in the
review form.  Not a huge problem, but I can't figure out why it is

Any ideas?  I've scoured my code.  There's nothing that deliberately
overwrites info in the $_SESSION array on the review page.  All I can
figure is it must have something to do with the way FOREACH operates.
Any help is appreciated,


Re: $_SESSION Array

Tom wrote:
Quoted text here. Click to load it

I don't know, why you have to mess up with session for form. Here is
my old version of form processing script <
which may be helpful sometimes.

<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: http://rajeshanbiah.blogspot.com /

Re: $_SESSION Array

Update: I've isolated the problem.

I put var_dumps for both arrays right before and after the extract()

It's the extract function that's doing it.  When the server runs
extract($DISPLAY) -- it would appear -- it takes the values from the
DISPLAY array and puts them in $_SESSION.

Peculiar -- this couldn't be how it's supposed to work, is it?

Just looked up extract() in the manual:


Maybe if I used one of the extract_type or prefix parameters, it would
stop this?

Re: $_SESSION Array

Quoted text here. Click to load it


Quoted text here. Click to load it

Your code here looks like it will do what you are intending.  The only two
ways to overwrite variables in the session value are to explicitly say
$_SESSION[$something] = $somethingelse, or to use foreach() like this (in
PHP 5):

foreach($arr as &$value) {
    //do something to value

You said that you have scoured your code for sets on the review page; I
would also check FORM.PHP to see where you are setting anything.  I assume
that, like most people, you aren't using the php 5 syntax I mentioned, so
search for all places where you set $_SESSION variables directly. You may
have to post some more code for us to help you out.

Hope this helps.


Re: $_SESSION Array

Thanks for the reply, Noah.

I had checked my code in FORM.PHP, too, so nothing in there should be
causing the problem.

To make sure, I added var_dum for the _SESSION and DISPLAY arrays at
the beginning and end of my REVIEW.PHP page.  At the beginning of the
page, DISPLAY is null and the variables in _SESSION for the fields that
weren't filled in on FORM.PHP are empty (or "") -- as expected.

The only operation I run on the _SESSION array after that is that
FOREACH statement described above.

At the end of the page, all the empty _SESSION variables are
repopulated with the BLANK string -- not as expected.
This is all being done in PHP 4.3.4.


Site Timeline