proggR Posted April 9, 2011 Share Posted April 9, 2011 I think I've gotten as far as I can with my troubleshooting so hopefully someone can help me here. I have a constructor for a controller class that receives the $_SESSION array from index.php by reference. An instance variable is then created when references this references so that any changes made to this instance variable update the real $_SESSION variable. This was working until it mysteriously stopped after not touching it for a few hours. The local $this->session variable is working because it completes some tasks using it. I've also hardcoded values into the session from the index.php file and when they are hard coded the session works as you would expect so it seems to just not be connecting this local variable to the $_SESSION array anymore for some reason. index.php snippet: session_start(); $action = 'login'; $controller = new UserController($_POST,$_SESSION); call_user_func(array($controller,$action)); constructor and login action from UserController (constructor is actually from a base controller but that shouldn't matter). Also the activeSession() function that checks if a session is valid and kicks the user if its not (which is what happens when anything is tried after logging in): function __construct($post,&$session){ $this->post = $post; $this->session = &$session; $this->view = 'views/login.php'; $this->title = ''; $this->error = ''; } function login(){ try{ $user = $this->validateString($this->post['username'],'Username'); $pass = $this->validateString($this->post['password'],'Password'); $dao = new UserData(); $this->session['userid'] = $dao->login($user, $pass); $this->session['username']=$user; $dao = new PostData(); $this->posts = $dao->getFeedPosts($this->session['userid']); $this->view = 'views/posts.php'; $this->title = 'Post Feed'; } catch(Exception $e){ $this->handleException($e); } } function activeSession(){ if(!empty($this->session['userid'])) return true; else return false; } Any help would be amazing. I really don't know how this stopped working. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/233162-_session-passed-by-reference-not-being-updated/ Share on other sites More sharing options...
proggR Posted April 9, 2011 Author Share Posted April 9, 2011 After playing around with it some more the reference does indeed seem to be working. If I print_r($_SESSION) right after session_start() it is an empty array. If I print_r($_SESSION) after calling the action method of my controller, it prints an array with the expected values so the reference is being made and $_SESSION is being updated. Its just not retaining that between pages for some reason. Again, any help would be great. I have no clue why this broke. Quote Link to comment https://forums.phpfreaks.com/topic/233162-_session-passed-by-reference-not-being-updated/#findComment-1199108 Share on other sites More sharing options...
proggR Posted April 9, 2011 Author Share Posted April 9, 2011 Turns out I had to tag session_write_close(); at the bottom of the index page. No clue why. I've never had to in the past and, like I said, it was working without it until it just stopped working. Oh well. Nightmare over. Quote Link to comment https://forums.phpfreaks.com/topic/233162-_session-passed-by-reference-not-being-updated/#findComment-1199111 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.