Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimToronto's avatar

Session in 4.2 not persisting

I've been reading through all the threads on session issues and haven't found one quite like mine. I think perhaps I have a misundestanding of how the page loads work.

I have a page where I assign an array value to a session variable (tagList) in my getIndex() function:

public function getIndex()
        {
            $getTag = Request::query('tag');
            $tagTable = DB::table('polaroids')
                                 ->join('polaroid_tag', 'polaroids.id', '=', 'polaroid_tag.polaroid_id')
                                 ->select(DB::raw('filename, polaroids.id as id, polaroid_tag.tag_id as tagid'))
                                 ->where('polaroid_tag.tag_id', '=', $getTag)
                                 ->orderByRaw('RAND()')->get();
            
            $data = array_map(function($object){
                return (array) $object;
            }, $tagTable);
            Session::put('tagList', $data);

On that page I have a jquery function which calls anther function from the same controller to swap out content on part of the page, that function is:

public function getDetails()
            {
                $getPos = Request::query('position');
                $actualPos = Session::get('tagList');
                $firstPos = $actualPos[$getPos]['id'];
                
                $tagTable = DB::table('polaroids')
                                     ->join('polaroid_tag', 'polaroids.id', '=', 'polaroid_tag.polaroid_id')
                                     ->select(DB::raw('filename, polaroids.id as id, polaroid_tag.tag_id as tagid'))
                                     ->where('polaroids.id', '=', $firstPos)
                                     ->get();
                $tagTable1 = DB::table('tags')
                                     ->join('polaroid_tag', 'tags.id', '=', 'polaroid_tag.tag_id')
                                     ->select(DB::raw('tag, tags.id as id'))
                                     ->where('polaroid_tag.polaroid_id', '=', $firstPos)
                                     ->orderByRaw('RAND()')->get();
                $this->layout = View::make('tag.details')->with(compact('tagTable'))->with(compact('tagTable1'));
            }

Here is the strange part, if I load my index page just once it will sometimes let me call the getDetails function one or twice before it starts giving me null responses and in testing it's because it shows my Session variable tagList as empty. However if load my index page once, then enter the full address /details?position=x, I can change x everytime and it still works, but if I go back to index and click on my next button it clears the session variable again. Any help or points in the right direction would be appreciated.

Thanks

0 likes
4 replies
TimToronto's avatar

Thanks for the suggestions, they don't seem to apply though. I have not started my own session, but instead am using the Laravel 4.2 installatio one already included in app.php.

I did what one of the suggestions said above about creating a test session variable and the same issue occurs. The session variable seems to work for several pages then all of a sudden it is empty, even though I'm browsing through the exact same pages.

TimToronto's avatar

Here is what ended up working for me (not sure why). I used the Session Variable in the controller but not actually in the view. By echoing the variable in my view it made everything work, but because I don't need that info displayed I just hid it with CSS. Doesn't seem like a good solution, but it is a solution.

Please or to participate in this conversation.