Connecting Tech Pros Worldwide Help | Site Map

$_SESSION issue

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 21st, 2005, 06:35 PM
Tom
Guest
 
Posts: n/a
Default $_SESSION issue

I put together a code that checks for 2 post variables then stores them
in a multi-dim $_SESSION array, something like this:

If ( isset($_POST['value_a'] && isset($_POST['value_b']) )
{
$_SESSION['value']['a'] = $_POST['value_a'];
$_SESSION['value']['b'] = $_POST['value_b'];
}

The problem was that $_SESSION['value']['b'] was being overwritten with
the value for 'a' so that both $_SESSION['value'] vars ended up being
the same (i.e. $_POST['value_b']. I thoroughly tested it to confirm
that this was what happening.

I solved the problem by changing the $_SESSION vars to
$_SESSION['value_a'] and $_SESSION['value_b'].

This was PHP 5, the WAMP package -- I know it's not the latest update
of PHP 5.

My question: is this a bug (perhaps since fixed) or is there a reason
this would be expected behavior?

Thanks,

Tom
http://phosphorusandlime.blogspot.com/


  #2  
Old December 21st, 2005, 06:45 PM
Justin Koivisto
Guest
 
Posts: n/a
Default Re: $_SESSION issue

Tom wrote:[color=blue]
> I put together a code that checks for 2 post variables then stores them
> in a multi-dim $_SESSION array, something like this:
>
> If ( isset($_POST['value_a'] && isset($_POST['value_b']) )
> {
> $_SESSION['value']['a'] = $_POST['value_a'];
> $_SESSION['value']['b'] = $_POST['value_b'];
> }
>
> The problem was that $_SESSION['value']['b'] was being overwritten with
> the value for 'a' so that both $_SESSION['value'] vars ended up being
> the same (i.e. $_POST['value_b']. I thoroughly tested it to confirm
> that this was what happening.
>
> I solved the problem by changing the $_SESSION vars to
> $_SESSION['value_a'] and $_SESSION['value_b'].
>
> This was PHP 5, the WAMP package -- I know it's not the latest update
> of PHP 5.
>
> My question: is this a bug (perhaps since fixed) or is there a reason
> this would be expected behavior?[/color]

The only way that I forsee this being expected behavior is if
$_POST['value_a'] == $_POST['value_b']

Usually when I am assigning an array to a session key, I will use:

$_SESSION['key'] = array('a'=>$a, 'b'=>$b);

I have never had any problems using either method, and tested that on my
two 5.1.1 installs which worked fine.

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
  #3  
Old December 22nd, 2005, 09:25 AM
Peter Fox
Guest
 
Posts: n/a
Default Re: $_SESSION issue

Following on from Tom's message. . .[color=blue]
>I put together a code that checks for 2 post variables then stores them
>in a multi-dim $_SESSION array, something like this:
>
>If ( isset($_POST['value_a'] && isset($_POST['value_b']) )
>{
> $_SESSION['value']['a'] = $_POST['value_a'];
> $_SESSION['value']['b'] = $_POST['value_b'];
>}
>
>The problem was that $_SESSION['value']['b'] was being overwritten with
>the value for 'a' so that both $_SESSION['value'] vars ended up being
>the same (i.e. $_POST['value_b']. I thoroughly tested it to confirm
>that this was what happening.[/color]

The following code works 'as advertised':
<?php
$SES['value']['a'] = 'va';
$SES['value']['b'] = 'vb';
print_r($SES);
?>
to give
Array ( [value] => Array ( [a] => va [b] => vb ) )

It works with $_SESSION too.

Are you operating in a foreach loop? If so this is a Gotcha.

Although not particularly related to this problem but in a similar vein:
Look up "variable variables" in the manual.

// convert $ay['foo'] into $foo
foreach($ay as $key => $val) {$$key = $ay[$key];}






--
PETER FOX Not the same since the bookshop idea was shelved
peterfox@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.