How to start sessions in slim?

I’ve been trying to understand how slim works. I want to know how to start session on a certain user type logs in. Please help me this is my final requirement.

Here’s my index.php code for loginUser.

Heres my JS code.

and heres my html code for Login.

Hello @csheantel

I’ve been trying to understand how slim works.

You could start with this Slim tutorial to get the basics.

I want to know how to start session on a certain user type logs in.

Make sure, that the session is getting started for each request. For this purpose the best would be to start the session within a Middeware.

Example file: src/Middleare/SessionMiddleware .php

<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class SessionMiddleware implements MiddlewareInterface
{

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }

        $response = $handler->handle($request);
        session_write_close();

        return $response;
    }
}

Although this is not your answer but It may help you to write better code.

Fe suggestions:

  1. Use a good code editor (VS Code recommended)
  2. Use Code Formatter