private function attachSessionCookie(ResponseInterface $response, int $time): ResponseInterface
    {
        // Get the session id, name and the cookie options.
        $id = session_id();
        $name = session_name();
        $options = session_get_cookie_params();

        // Create a cookie header.
        $header = urlencode($name) . '=' . urlencode($id);

        if ($options['lifetime'] > 0) {

            $expires = gmdate(self::DATE_FORMAT, $time + $options['lifetime']);

            $header .= "; expires={$expires}; max-age={$options['lifetime']}";

        }

        if ($options['path']) $header .= "; path={$options['path']}";
        if ($options['domain']) $header .= "; domain={$options['domain']}";
        if ($options['secure']) $header .= '; secure';
        if ($options['httponly']) $header .= '; httponly';

        // Return a new response with the cookie header.
        return $response->withAddedHeader('set-cookie', $header);
    }