meltingpoint Posted January 4, 2010 Share Posted January 4, 2010 How does one code sessions with register_globals set to off? <? session_start(); $_SESSION['name'] = 'Joe'; ?> .........now when I have session_start() at the top of each and every page- when I call on any subsequent pages echo "$_SESSION['name']"; I get nothing. With register_globals set to ON- it will echo - Joe What am I missing. Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/ Share on other sites More sharing options...
teamatomic Posted January 4, 2010 Share Posted January 4, 2010 The only thing with globals on is that $_SESSION['name'] = 'joe'; would be accessible with $name without doing $name = $_SESSION['name']; But to echo out what you want you need to: echo "{$_SESSION['name']}"; enclose the session var in curly brackets HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-987931 Share on other sites More sharing options...
meltingpoint Posted January 4, 2010 Author Share Posted January 4, 2010 Ok- I did that and it did echo out correctly. But why the curly brackets? Also- I have a login script that I can use with globals on- but not with globals off. IN other words the variable is not being passed from page to page. Obviously I am not echoing them out to each page- put my script does check a flat file data base and does not allow access if the session variable does not match. Turn globals off and it triggers the "You are not a member" message. So what is the correct way to call declare a $_SESSION variable and have it accessible from page to page? Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-987933 Share on other sites More sharing options...
teamatomic Posted January 4, 2010 Share Posted January 4, 2010 page1: session_start(); $_SESSION['foo'] = 'bar' page2" session_start(); echo "{$_SESSION['foo']}"; read my first answer about globals and $_SESSION['name'] and $name. the reason your script works with globals on is that is uses $name instead of $_SESSION['name'] or $_POST['name'] or $_GET['name']. I dont know how to make it any clearer. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-987936 Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2010 Share Posted January 4, 2010 echo "$_SESSION['name']"; produces a fatal parse error, having nothing to do with the register_globals setting and therefore cannot be your actual code. It would take seeing your actual code to be able to tell you why it is not working when register_globals are off. Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-987952 Share on other sites More sharing options...
meltingpoint Posted January 4, 2010 Author Share Posted January 4, 2010 on my authenticate page I have the $_SESSION variables declared; $_SESSION['Xuser'] = $Xuser; now I call to see if the session variable $_SESSION['Xuser'] is set; if(!isset($_SESSION['Xuser'])) { echo "Sorry your not a member"; } else { .....continue on } with register_globals on- it works. With register_globals off- it triggers the "Sorry your not a member" Sorry if I am being dense here- but I am missing something. So how do I use the decalred session variable. And also- in the previous posts, what function did the curly brackets have? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-987987 Share on other sites More sharing options...
gizmola Posted January 4, 2010 Share Posted January 4, 2010 We can only suspect, because this isn't your real code, and you're quite probably omitting a key detail that would explain things better. A few things to know: The isset() language construct can be very confusing if you don't understand how it works. Admittedly the manual doesn't do the best job with it. This is probably more as a comment because it doesn't really explain what you're seeing, but doing what you're doing isn't the best idea, because the minute you do the $_SESSION['Xuser'] = assignment, unless you are doing an unset at some point, or you are assigning to null, isset is going to be true. That seems more a problem with your working code, since you will allow someone to get into the member system, even if $Xuser is an empty string. $foo = ""; if (isset($foo)) { echo "Foo isset"; } else { echo "Foo is not set"; } So what I'm guessing the real problem here is that what register globals is changing, is that it's hiding a flaw in your form handling code, where you are getting whatever $_POST or $_GET variables during the login. Perhaps you have a typo or some other small detail wrong, and register globals is hiding that mistake. It has nothing to do with how $_SESSION works or doesn't, as PFMaBiSmAd already explained. With that said -- register globals is really bad, opens up huge security holes and is deprecated. It doesn't do anything magic other than to convert all the variables that come in from the environment into global variables. Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-988024 Share on other sites More sharing options...
meltingpoint Posted January 4, 2010 Author Share Posted January 4, 2010 Thanks guys- I appreciate the help. I will work on what you have shown me and get it worked out. If I run into further problems- I will be back. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-988090 Share on other sites More sharing options...
teamatomic Posted January 4, 2010 Share Posted January 4, 2010 I dont know what the curly brackets do. I never though much about why, I just know it is and know I have to use it as stated or it just wont work. As stated by someone else, show us the code. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187082-register_globals-off-and-my-sessions-wont-work/#findComment-988109 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.