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 Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/ 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. Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789912 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 Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789916 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. Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789917 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 Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789920 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. Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789923 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 Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789925 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? Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789930 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 Link to comment https://forums.phpfreaks.com/topic/150405-solved-does-session_start-have-to-be-called-inside-a-function/#findComment-789937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.