Jump to content

Recommended Posts

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.