Forum Moderators: coopster

Message Too Old, No Replies

Getting form variables into session vars

Looping through POST array

         

Reflection

12:07 am on Jan 8, 2004 (gmt 0)

10+ Year Member



Hi all I was just looking for a little help on this.

First question is if this is a good idea or not. What I want to do is loop through my $_POST vars array and I would like each variable to be stored in a Session variable of the same name. Anything wrong with doing this?

Secondly Im not sure exactly how to code it as I am new to php.

I have something like:

foreach($HTTP_POST_VARS as $field_name=>$field_value){
$_SESSION['$field_name'] = $field_value;
}

Is that close? :)

Thanks for your help.

Reflection

12:17 am on Jan 8, 2004 (gmt 0)

10+ Year Member



Ok I think I just answered my own question :).

This appears to work:

foreach($_POST as $key => $value){
$_SESSION[$key] = $value;
}

Slade

1:56 am on Jan 8, 2004 (gmt 0)

10+ Year Member



I use the following code, to load all the passed vars into a nice little attribute package (like from fusebox):

 $attributes = array();
// fill post/get vars, GET overwrites POST
$attributes = array_merge($attributes,$HTTP_POST_VARS, $HTTP_GET_VARS);

I don't pass it to session directly, but you could just as easily use it to load up everything into there.

ergophobe

6:26 am on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If array attributes is empty, as in the current example

$attributes = array_merge($attributes, $HTTP_POST_VARS, $HTTP_GET_VARS)

is the same as

$attributes = $_REQUEST

whether post overwrites get or the other way around is set by ... which directive now?

coopster

2:13 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Predefined variables [php.net]

$_REQUEST [php.net]

Variables provided to the script via the GET, POST, and COOKIE input mechanisms, and which therefore cannot be trusted. The presence and order of variable inclusion in this array is defined according to the PHP variables_order [php.net] configuration directive. This array has no direct analogue in versions of PHP prior to 4.1.0. See also import_request_variables() [us3.php.net].

ergophobe

12:23 am on Jan 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




variables_order

That's the one I was thinking of, but was too lazy to look up.