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

Daniel1836's avatar

How to check if session is set?

I'm using Symfony 3.

class UserProfileSubscriber implements EventSubscriber
{

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;

    }

    public function getSubscribedEvents()
    {
        return array(
            SecurityEvents::INTERACTIVE_LOGIN => 'interactiveLogin',
            KernelEvents::REQUEST => 'request',
        );
    }

    public function interactiveLogin(InteractiveLoginEvent $event, EntityManagerInterface $em)
    {

        $user = $this->em->find(User::class, $userId);
        if (!$user->getStreetAddress1())
        {
            $session = $event->getRequest()->getSession();
            $session->set('userNeedsProfileUpdated', true);
        }
    }

    public function request(KernelEvent $event)
    {

        $session = $event->getRequest()->getSession();
        $session->get('userNeedsProfileUpdated');
        if($session->has("userNeedsProfileUpdated")) {
            $event->setResponse(new RedirectResponse('user_profile'));
        }
    }
}

How do I check if "userNeedsProfileUpdated" is set to true in the session? I'm using the has method, but I'm not sure I'm using it right. Then set it to false to prevent a loop.

I need to listen for logins and then check if certain fields in my form are set, if they aren't redirect to the form view page.

I'm not sure my code is correct so far.

Thanks

0 likes
1 reply
vincent15000's avatar

Here it's a Laravel forum ... and even if Laravel is based on Symfony, I'm not sure to be able to help you.

Please or to participate in this conversation.