Why doesn’t symfony 7 cookies not work, but vanilla php do?

In my little project I want a user imediadly log out if the user was banned (By Admin or by changing the Database with a tool). I tried to set a cookie with symfony but it dosen’t work. But if I use vanilla PHP for this solution it works as requested.
For this I use an eventlistener named “UserStatusListener.php” and in my LoginController I have a function to handle the logout-message (The reason why the user was logged out).

Maybe one of you find the issue. I want to use the Framework instead of vanilla PHP.

Here my EventListener:

<?php
// src/EventListener/UserStatusListener.php

namespace AppEventListener;

namespace AppEventListener;

use AppRepositoryUserRepository;
use SymfonyComponentHttpFoundationCookie;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpKernelEventRequestEvent;
use SymfonyComponentRoutingRouterInterface;
use SymfonyBundleSecurityBundleSecurity;
use SymfonyComponentHttpFoundationResponse;

class UserStatusListener
{
    private $userRepository;
    private $security;
    private $router;

    public function __construct(UserRepository $userRepository, Security $security, RouterInterface $router)
    {
        $this->userRepository = $userRepository;
        $this->security = $security;
        $this->router = $router;
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        $user = $this->security->getUser();

        if ($user) {
            $updatedUser = $this->userRepository->find($user->getId());

            if ($updatedUser && $updatedUser->getBannedUntil() !== null) {
                $now = new DateTime();
                if ($now < $updatedUser->getBannedUntil()) {
                    // Setze ein Cookie mit der Benutzer-ID
                    // $cookie = new Cookie('banned_user_id', $user->getId(), time() + 3600, '/', 'stage.hhb.kighlander.de', true); // 1 Stunde gültig
                    setcookie('banned_user_id', $user->getId(), time() + 3600, '/', '.kighlander.de', true);
                    // Erstelle eine neue Response, wenn keine vorhanden ist
                    if (!$event->getResponse()) {
                        $event->setResponse(new Response());
                    }

                    // Setze das Cookie in die Response-Header
                    // $event->getResponse()->headers->setCookie($cookie);
                    // Benutzer ausloggen und zur Logout-Methode weiterleiten
                    $event->setResponse(new RedirectResponse($this->router->generate('app_logout')));
                    return; // Beende die Methode hier
                }
            }
        }
    }
}

And here my LoginController:

<?php
// src/Controller/LoginController.php

namespace AppController;

use AppRepositoryUserRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequestStack;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAttributeRoute;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationUtils;

class LoginController extends AbstractController
{
    private $requestStack;
    private $userRepository;

    public function __construct(RequestStack $requestStack, UserRepository $userRepository)
    {
        $this->requestStack = $requestStack;
        $this->userRepository = $userRepository;
    }

    #[Route('/login', name: 'app_login')]
    public function index(
        AuthenticationUtils $utils
    ): Response
    {
        $lastUsername = $utils->getLastUsername();
        $error = $utils->getLastAuthenticationError();

        return $this->render('login/index.html.twig', [
            'lastUsername' => $lastUsername,
            'error' => $error
        ]);
    }

    #[Route('/logout', name: 'app_logout')]
    public function logout()
    {

    }

    #[Route('/logout_message', name: 'app_logout_message')]
    public function logoutMessage(): Response
    {
        // $bannedUserId = $this->requestStack->getCurrentRequest()->cookies->get('banned_user_id');
        $bannedUserId = $_COOKIE['banned_user_id'] ?? null;
        if ($bannedUserId) {
            // Benutzer aus der Datenbank abrufen
            $user = $this->userRepository->find($bannedUserId);

            if ($user && $user->getBannedUntil() !== null) {
                $now = new DateTime();
                if ($now < $user->getBannedUntil()) {
                    // Setze eine Warnung für den gebannten Benutzer
                    $this->addFlash('warning', 'Du bist gebannt. Bitte kontaktiere den Support.');
                }
            }

            // Lösche das Cookie
            $response = $this->redirectToRoute('app_micro_post');
            $response->headers->clearCookie('banned_user_id');
            return $response;
        } else {
            // Setze die Erfolgsnachricht für den normalen Logout
            $this->addFlash('success', 'Du hast Dich erfolgreich ausgeloggt.');
        }

        return $this->redirectToRoute('app_micro_post');
    }

}

At least a part of “service.yaml” for registration the EventListener

services:
  AppEventListenerUserStatusListener:
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

I outcomment the framework-version and the implemented vanilla-version works.

Thank you 😉

To test the code I created a test-user, logged in (without a ban), change to phpmyadmin and set a ban_until one day in future, change to the logged in user, make a click to an other part of the app and I was logged out with the “normal” logout-message.

After I made the changes to vanilla php I got the correct logout message “Du bist gebannt. Bitte kontaktiere den Support” (Sorry, my site is in german 😉 )

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật