calmchess Posted December 11, 2009 Share Posted December 11, 2009 I create a session $_SESSION['RYXQeab7']="test"......inside a function but i can't access the session from anyother page.....however if i set that same session outside the function all is well.....is there some sort of scope issues with a function....i don't know if it helps or not but the function in question is brought onto the page via a include statement........what is my problem? Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/ Share on other sites More sharing options...
mrMarcus Posted December 11, 2009 Share Posted December 11, 2009 you need to have: session_start(); before you create a session variable with $_SESSION or send headers. pretty safe to just put it at the top of each script/page that will be using sessions. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975146 Share on other sites More sharing options...
calmchess Posted December 11, 2009 Author Share Posted December 11, 2009 thanks for your time....i have session_start();......i can set and use sessions fine on all pages except for the session being set inside the function. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975150 Share on other sites More sharing options...
trq Posted December 11, 2009 Share Posted December 11, 2009 We can't help without seeing the relevant code. The $_SESSION array is a super global, so can be accessed from anywhere. You must being doing something wrong. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975154 Share on other sites More sharing options...
calmchess Posted December 11, 2009 Author Share Posted December 11, 2009 all the code is several hundred lines i put the relevent cod that starts the session below. function setname($id){ //select username from page0.page0 where id = $id; $_SESSION['RYXQeab7']="test"; $getname = mysql_query("select username from page0.page0 where activ = $id") or die(mysql_error()); $row = mysql_fetch_array($getname); $uname = $row['username']; mysql_query("INSERT IGNORE INTO page0.payroll (username)VALUES('$uname')") or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975156 Share on other sites More sharing options...
mrMarcus Posted December 11, 2009 Share Posted December 11, 2009 sorry, just combed over your post. just assumed it was another "you just need to add session_start(); to the top of the script". $_SESSION is a superglobal which means it has a global scope. you need to make sure that the function is being called/executed for the $_SESSION to be set, and that session_start() is at the top of each script. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975157 Share on other sites More sharing options...
calmchess Posted December 11, 2009 Author Share Posted December 11, 2009 well this is strange you guys don't have any answers that i haven't thought of so it must mean something truly strange is happening. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975158 Share on other sites More sharing options...
calmchess Posted December 11, 2009 Author Share Posted December 11, 2009 well it was session start the scope for session_start was weirding me out the include file has sesssion_start but i had to call it again in page that calls the function in th include file......just take me word for it it was weird and had everything to do with the way i stucture my document thanks guys.. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975176 Share on other sites More sharing options...
PFMaBiSmAd Posted December 11, 2009 Share Posted December 11, 2009 Your code is either not calling the function to set the value (have you tried echoing something inside of the function as confirmation), is overwriting the value (have you echoed it right after the function call), you have something like a session_write_close() statement before the point where the function is being called (what is you code doing that could affect the existence of that session variable), register_globals are on (I hope not, but you should be aware if they are on and what the ramifications are of them being on) and you have a get/post/cookie or program variable by that same name, short open tags are being used in your code so that some of the php code is not really php code, your including code using a URL instead of a file system path so the results are not really what you think they are, or ... There are literally a couple dozen different possible causes for the symptom, so you would really need to post the full code involved, with any included files and showing the php tags being used, for both the page you are setting the variable and the page where it does not contain the expected value. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975180 Share on other sites More sharing options...
calmchess Posted December 11, 2009 Author Share Posted December 11, 2009 PFMaBiSmAd I solved the problem it was session_start(); needed to be called in a strange place due to my document structure. Thanks for you time though....i do appriciate u all. Quote Link to comment https://forums.phpfreaks.com/topic/184719-session-inside-function-not-being-set/#findComment-975203 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.