# How to start sessions in slim?

**URL:** <https://discourse.slimframework.com/t/how-to-start-sessions-in-slim/4807>\
**Category:** Questions\
**Created:** [May 11, 2021, 6:56pm UTC](https://discourse.slimframework.com/t/how-to-start-sessions-in-slim/4807 "2021-05-11T18:56:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![csheantel](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@csheantel](https://discourse.slimframework.com/u/csheantel)\
**Post date:** [May 11, 2021, 6:56pm UTC](https://discourse.slimframework.com/t/how-to-start-sessions-in-slim/4807/1 "2021-05-11T18:56:09Z")

</div>

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.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/6/60abb67748c940e60157041c0766c768f5555a7f.png)

Heres my JS code.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/f/fbd7767c38d4c7bd9884c765aeb3e47f4aa6e161.png)

and heres my html code for Login.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/2/227ee74edff15248a3a3e36ed7fdd4e87f7e8273.png)

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 11, 2021, 7:27pm UTC](https://discourse.slimframework.com/t/how-to-start-sessions-in-slim/4807/2 "2021-05-11T19:27:29Z")

</div>

Hello @csheantel

> I’ve been trying to understand how slim works.

You could start with this [Slim tutorial](https://odan.github.io/2019/11/05/slim4-tutorial.html) 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
<?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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![raziul](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/raziul/32/1161_2.png) [@raziul](https://discourse.slimframework.com/u/raziul)\
**Post date:** [May 11, 2021, 7:40pm UTC](https://discourse.slimframework.com/t/how-to-start-sessions-in-slim/4807/3 "2021-05-11T19:40:35Z")

</div>

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
