public static function generateSessionId($length = 40)
    {
        if ($length < 1) {
            throw new \InvalidArgumentException('Length should be >= 1');
        }

        $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
        $numChars = 36;

        $bytes = random_bytes($length);
        $pos = 0;
        $result = '';
        for ($i = 0; $i < $length; $i++) {
            $pos = ($pos + ord($bytes[$i])) % $numChars;
            $result .= $chars[$pos];
        }

        return $result;
    }