Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problems with setting sessions via post, keeps overwriting and clearing

Posted on 2008-06-19
4
Medium Priority
?
263 Views
Last Modified: 2010-04-21
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']['thisFund']
$_SESSION['details']['thisAction']

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
0
Comment
Question by:Neil Thompson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21821042
You can use this function to merge the arrays without overwriding.

See http://www.php.net/session for more about sessions
function merge(&$dst, $src) {
    if (!is_array($src)) {
        $dst = $src;
        return;
    }
    foreach($src as $k => $v) {
        merge($dst[$k], $v);
    }
}
 
$_SESSION = array();
$_POST1 = array('details' => array('a' => 'a', 'b' => 'b'));
$_POST2 = array('details' => array('c' => 'c'));
 
merge($_SESSION, $_POST1);
merge($_SESSION, $_POST2);
 
var_dump($_SESSION);

Open in new window

0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 21826618
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
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 21826751
don't clear $_SESSION. init $_SESSION on the first page, then make sure you have always
session_start() called before you merge the infos on the following pages.

it was in my example just for init the variable.
0
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 31468710
Sorry, I'm bit of a cut and paster!
Many thanks
Neil
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question