Page 1 of 1

Secret to using $_POST and $_SESSION variables

Posted: Thu Apr 14, 2005 11:03 pm
by steedvlx
Hi again,

For today's hard-to-find-the-answer amateur Q&A, I would like to consider the esteemed $_POST and $_SESSION variables.

I need to trap some of the $_POST variables and use them in local manipulations without altering their values. I thought it would be straightforward, but these case tell me I'm going to run into a problem.

I tried

Code: Select all

$new_action = $_POST['action'];
if ($admin_auth == "TRUE" && $new_action == 3) {
.......
and

Code: Select all

$user_ident = $_SESSION['userid']
if ($user_ident = $localdata[user_id]) {
......
to no avail. BUT, if I change the same lines in the code to...

Code: Select all

if ($admin_auth == "TRUE" && $_POST['action'] == 3){
.....
and

Code: Select all

if $_SESSION['user_id'] = $localdata[user_id]) {
......
Can someone point me to an explanation why the first sets of code don't work but the second set does? I assume it has to do with rules regarding the assignment of global (?) variables. But, I cannot find the rules in writing.

Thanks,

Posted: Thu Apr 14, 2005 11:13 pm
by feyd
look up variable scope.

Posted: Fri Apr 15, 2005 1:30 am
by wyred
I noticed you used a single = sign when comparing variables in your second and fourth code sample. Fix that first.

Posted: Fri Apr 15, 2005 3:39 am
by steedvlx
wyred wrote:I noticed you used a single = sign when comparing variables in your second and fourth code sample. Fix that first.
Thanks for the look. I did see that after posting and changed it.

Posted: Fri Apr 15, 2005 3:58 am
by steedvlx
feyd wrote:look up variable scope.
Thanks! That wasn'T exactly an intuitive keyword for me to search. :)
This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_POST; to access it within functions or methods, as you do with $HTTP_POST_VARS.
The above would seem to indicate that in PHP4.1~, All I have to do is use it just like a local-scope variable without any special handlers or container addressing. So, if I have this right.

If I wanted to clear or alter say,

Code: Select all

$_POST['action']
, I could simply write

Code: Select all

$action = 0;
and it would be essentially cleared (but still exist) in the $_POST array. So, I could use it 'as-is' in any calculation I want just like any other variable. No global... no nothing.

Am I correct about this? My just-completed experimentation seems to back my conclusions up. But, I still get the hives when doing too much manipulation of globals like this. It just seems like bad form to me for some reason. :?

Thanks,