Session variables problem

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
ulezacvenje
Forum Newbie
Posts: 2
Joined: Wed Jul 14, 2010 7:59 am

Session variables problem

Post by ulezacvenje »

Hello!

I've noticed this strange PHP behavior. Following example shows that these two variables, $_SESSION['name'] and $name are actually single variable. When one of them is given some value, the other takes this same value?!

Code: Select all

session_start();
if (!session_is_registered('name')) session_register('name');
echo 'before:' . $_SESSION['name'];
$name = 'value 1';
echo '<br>after:' . $_SESSION['name']; // this should echo only 'after:' but it echoes 'after:value 1'
$_SESSION['name'] = 'value 2';
echo '<br>finally:' . $name; // this should echo 'finally:value 1' but it echoes 'finally:value 2'
This strange behavior had caused me many headaches in the past, until I have noticed it. I tried to google it but I don't know if PHP should behave like that or not?

Thank you!
User avatar
Ragnis
Forum Commoner
Posts: 31
Joined: Thu Nov 13, 2008 12:35 pm
Location: Saaremaa, Estonia, Europe, Asia, Planet Earth, The Solar System, Milky way.

Re: Session variables problem

Post by Ragnis »

From your php.ini file set register_globals = Off
ulezacvenje
Forum Newbie
Posts: 2
Joined: Wed Jul 14, 2010 7:59 am

Re: Session variables problem

Post by ulezacvenje »

Thank you, Ragnis. But before I send register_globals = Off request to my site's host, I'd like to know whether variables from my included PHP scripts will be visible from the main PHP file and vice versa?
anilnakkani
Forum Newbie
Posts: 5
Joined: Wed Jul 21, 2010 2:16 am
Location: Hyderabad

Re: Session variables problem

Post by anilnakkani »

As per security issue register_globals should be off, other all our POST,GET,and COOKIE data is visable for all.
Always register_globals = off; in php.ini file

Thanks
Anil.N
Thought Radius - Experts for Partners
Post Reply