Secret to using $_POST and $_SESSION variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Secret to using $_POST and $_SESSION variables

Post 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,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look up variable scope.
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post by wyred »

I noticed you used a single = sign when comparing variables in your second and fourth code sample. Fix that first.
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post 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.
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post 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,
Post Reply