Neil Thompson
asked on
problems with setting sessions via post, keeps overwriting and clearing
Hi
(not really sure if my ideas are safe or the best way so expert guidance would be very well received)
I'm trying to set session values using a post to a controller page that basically receives all the post commands then send the page on (bit like struts in Java)
foreach ($_POST as $var => $val) {
$_SESSION[$var] = $val;
}
My forms pages contain form input elements such as:
<input type="hidden" name="details[thisFund]" value="ctx" />
<input type="hidden" name="details[thisAction]" value="_details" />
Which would create sessions such as:
$_SESSION['details']['this Fund']
$_SESSION['details']['this Action']
All seems to work ok, but if the post gets another value such as
<input type="hidden" name="details[name]" value="neil" />
this would erase all my current details[xxx][xxx] sessions.
Can someone advise me how to perhaps
1) check if the session exists first
2) if that particular one such as name[xx][xx] does already exist over write just that one
3) if the base exists e.g. name as a session just append it
Hope that makes sense, basically add if not there, amend if it is
Asking for your wisdom mighty ones!
Neil
(not really sure if my ideas are safe or the best way so expert guidance would be very well received)
I'm trying to set session values using a post to a controller page that basically receives all the post commands then send the page on (bit like struts in Java)
foreach ($_POST as $var => $val) {
$_SESSION[$var] = $val;
}
My forms pages contain form input elements such as:
<input type="hidden" name="details[thisFund]" value="ctx" />
<input type="hidden" name="details[thisAction]"
Which would create sessions such as:
$_SESSION['details']['this
$_SESSION['details']['this
All seems to work ok, but if the post gets another value such as
<input type="hidden" name="details[name]" value="neil" />
this would erase all my current details[xxx][xxx] sessions.
Can someone advise me how to perhaps
1) check if the session exists first
2) if that particular one such as name[xx][xx] does already exist over write just that one
3) if the base exists e.g. name as a session just append it
Hope that makes sense, basically add if not there, amend if it is
Asking for your wisdom mighty ones!
Neil
ASKER
Hi
I've tried your example using:
function merge(&$dst, $src) {
if (!is_array($src)) {
$dst = $src;
return;
}
foreach($src as $k => $v) {
merge($dst[$k], $v);
}
}
$_SESSION = array();
merge($_SESSION, $_POST);
And they didnt appear to set, am I doing it wrong?
Neil
I've tried your example using:
function merge(&$dst, $src) {
if (!is_array($src)) {
$dst = $src;
return;
}
foreach($src as $k => $v) {
merge($dst[$k], $v);
}
}
$_SESSION = array();
merge($_SESSION, $_POST);
And they didnt appear to set, am I doing it wrong?
Neil
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, I'm bit of a cut and paster!
Many thanks
Neil
Many thanks
Neil
See http://www.php.net/session for more about sessions
Open in new window