therealwesfoster Posted March 20, 2009 Share Posted March 20, 2009 If in a function I'm going to be working with sessions, does session_start() need to be called in the function? Or global $_SESSION? Please let me know Wes Quote Link to comment Share on other sites More sharing options...
.josh Posted March 20, 2009 Share Posted March 20, 2009 session_start() needs to be called before you use a session variable. Doesn't matter if it's inside or outside of a function. session variables are already global. If you assign something to a session variable without (or before) calling session_start(), it will act like a normal global variable for that page, but will not persist to other pages. Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 20, 2009 Author Share Posted March 20, 2009 Thanks for the reply I have a global header in which I call session_start(). Should I call this again in a function (later down the page) or no? Wes Quote Link to comment Share on other sites More sharing options...
trq Posted March 20, 2009 Share Posted March 20, 2009 If this so called global header has already been included there would be no need to include it again. Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 20, 2009 Author Share Posted March 20, 2009 Ok, so this simple script should output "test" <?php session_start(); $_SESSION['test'] = 'test'; function show(){ return $_SESSION['test']; } echo show(); ?> Wes Quote Link to comment Share on other sites More sharing options...
.josh Posted March 20, 2009 Share Posted March 20, 2009 Instead of c/p'ing it here and asking if it will work, you could always c/p it to a test.php and run it and find out. Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 20, 2009 Author Share Posted March 20, 2009 Instead of c/p'ing it here and asking if it will work, you could always c/p it to a test.php and run it and find out. 1. I didn't copy, I wrote it real quick. 2. I didn't ask if it would work, I just made a statement before marking the topic as solved. Wes Quote Link to comment Share on other sites More sharing options...
.josh Posted March 20, 2009 Share Posted March 20, 2009 hmm I coulda swore I saw a question mark in there, hence the statement. My bad. But for argument sake, for #1, I meant it in a more generic sense. As in, if you're going to take the time to put it in one place, why not just put it in another place, click the proverbial run button and see what happens? Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted March 20, 2009 Author Share Posted March 20, 2009 hmm I coulda swore I saw a question mark in there, hence the statement. My bad. But for argument sake, for #1, I meant it in a more generic sense. As in, if you're going to take the time to put it in one place, why not just put it in another place, click the proverbial run button and see what happens? 10-4. I wasn't offended Wes Quote Link to comment 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.