Can $_POST variables be modified in a function?
Can $_POST variables be modified in a function?
am 07.04.2008 20:46:19 von Kevin Audleman
Hello,
I'm working with a web store package which gives me specific places
where I can insert my code. I am trying to modify $_POST variables
after a form is submitted, and before the store actually processes
them. This is within a class function. Problem is, any $_POST variable
I modify is disappearing from the end result. I don't know if there's
a warning being thrown that the web store is suppressing or what, but
it's my modification of the values that seems to be screwing things
up. I came across a post in the PHP documentation that mentioned
something about superglobals not being able to be modified in
variables, but gave no solution. Does anybody know what the deal is,
and how I might be able to modify my $_POST variables?
Thanks,
Kevin
Re: Can $_POST variables be modified in a function?
am 08.04.2008 11:19:59 von Erwin Moller
Kevin Audleman schreef:
> Hello,
>
> I'm working with a web store package which gives me specific places
> where I can insert my code. I am trying to modify $_POST variables
> after a form is submitted, and before the store actually processes
> them. This is within a class function. Problem is, any $_POST variable
> I modify is disappearing from the end result. I don't know if there's
> a warning being thrown that the web store is suppressing or what, but
> it's my modification of the values that seems to be screwing things
> up. I came across a post in the PHP documentation that mentioned
> something about superglobals not being able to be modified in
> variables, but gave no solution. Does anybody know what the deal is,
> and how I might be able to modify my $_POST variables?
>
> Thanks,
> Kevin
Hi Kevin,
You can modify the $_POST array just as you like.
You must make a mistake elsewhere.
Here is an example that shows that:
----------------------------------------
Post before:
$_POST["test"] = "bla";
?>
after:
----------------------------------------
How are you reposting the data? Via Curl?
Regards,
Erwin Moller
Re: Can $_POST variables be modified in a function?
am 08.04.2008 18:48:03 von Kevin Audleman
Hello Erwin,
It's a little bit different. I'm trying to modify the $_POST variables
inside a function. When I modify $_POST inline like you show, it works
just fine. It's when it's within a function that the outcome is
screwy. My example would look like this:
function modifyPost() {
$_POST['var1'] = 'foo';
}
?>
Post before:
//Prints [var1] = foo
modifyPost();
?>
after:
// Prints [var1] = ''
Re: Can $_POST variables be modified in a function?
am 09.04.2008 09:23:08 von Erwin Moller
Kevin Audleman schreef:
> Hello Erwin,
Hi Kevin,
>
> It's a little bit different. I'm trying to modify the $_POST variables
> inside a function.
Doesn't matter. $_POST is a superglobal.
When I modify $_POST inline like you show, it works
> just fine. It's when it's within a function that the outcome is
> screwy.
It shouldn't be.
My example would look like this:
>
>
>
> function modifyPost() {
> $_POST['var1'] = 'foo';
> }
> ?>
>
> Post before:
>
>
> //Prints [var1] = foo
No, it does NOT.
POST should be empty there, no var1 is set yet.
[See my output below]
>
>
>
> modifyPost();
> ?>
>
> after:
>
>
> // Prints [var1] = ''
No it doesn't over here. [See my output below]
>
Your example outputs the following on my PHP5 machine:
Post before:
Array
(
)
after:
Array
(
[var1] => foo
)
Which is excactly what one expects when it comes to superglobals like
$_POST[].
Superglobal means you can access it anywhere from your code without
paying attention to scope, eg in a function.
Did you TRY your own code you posted anyway?
You must be doing something else (wrong) than this.....
Please test your own example above and tell us what the output is.
Regards,
Erwin Moller