Relationship netween $_SESSION and local 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
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Relationship netween $_SESSION and local variables

Post by Bill H »

Can someone explain why this happens:

Code: Select all

<?php
    print_r($_SESSION);              // $_SESSIONї'Assn'] = "none" at this point
    $Assn = $_POSTї'Number'];
    print_r($_SESSION);              // $_SESSIONї'Assn'] = "1150" at this point
?>
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

it looks like register_globals is turned on. can you check? that is a weird happening.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you registered $Assn as a session variable (and as mydimension pointed out got registered_globals on):

Code: Select all

session_register('Assn');
Code like the above and reg_globals on could cause this.

Mac
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Yes, register globals is turned on, but

No, I have not done "session_register('Assn');" anywhere in any file.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

while im still unsure about this, i think with register_globals on $_SESSION['Assn'] would be the same as $Assn. so setting one would change the other.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I'm with mydimension on this one, not entirely sure but if you've done:

Code: Select all

$_SESSIONї'Assn'] = 'something';
you've registered $Assn as a session variable because of register_globals being on. Basically you'll have to make sure that you use unique names for your session variables that you don't use anywhere else in your script except when setting or retrieving that session variable or turn reg_globals off.

Mac
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

One thing to understand with the new superglobals is that $_SESSION works exactly the way session_register() does. So if you use

Code: Select all

<?php
$_SESSIONї'Assn'] = 'something';
?>
then its the same as using

Code: Select all

<?php
$Assn = 'something';
session_register('Assn');
?>
Now due to register_globals being on, it has the same effect either way you do it, so $Assn will be the session variable. I hope that makes a little bit more sense. I also suggest that you read the manual a little more on sessions php.net/session
Post Reply