By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,777 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,777 IT Pros & Developers. It's quick & easy.

$_SESSION Array

P: n/a
Tom
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;
}
}
extract($DISPLAY);

--- 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
happening.

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,

Tom

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Tom wrote:
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:

<snip>

I don't know, why you have to mess up with session for form. Here is
my old version of form processing script <
http://groups.google.com/gr*******************************************@post ing.google.com>,
which may be helpful sometimes.

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

Jul 17 '05 #2

P: n/a
"Tom" <kl******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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] = "&laquo; BLANK &raquo;";
}
else {
$DISPLAY[$key] = $submission;
}
}
extract($DISPLAY);

--- end code ---
<snip>
Any help is appreciated,

Tom


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.

-Noah
Jul 17 '05 #3

P: n/a
Tom
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.

Tom

Jul 17 '05 #4

P: n/a
Tom
Update: I've isolated the problem.

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

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:

http://us2.php.net/extract

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

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.