My first question would be; why are you doing all this? You're basically fighting the framework in so many ways. Laravel would manage the session for you, that's why you'd use a framework. You're starting the session extremely late in the request lifecycle, which would likely interfere with Laravel, as you are using Laravel to pull data from the session.
My recommendation, if you're learning PHP, don't start with a framework, learn PHP. Then using a framework would make a lot more sense, as the framework would do a lot of things for you already.
Like, all of what you're doing is already built-in to Laravel, it's all documented in the authentication docs; https://laravel.com/docs/11.x/authentication
#Laravel 11X Session var not working
1 messages · Page 1 of 1 (latest)
In the second solution I used Laravel authentication system, I aint trying to fight it but it doesn't work. I am used to PHP, simply not to Laravel 11. Last time I used it, it was 4.2
And to answer your question on why I don't use what is already made like Jetstream is because it is a "homework" taking a Web specialisation course and they asked to do it ourselves, so I had in plan to do it like this and once it is working to put it in a middleware that will redirect to login from every page if there is no currently connected user
But the docs already explain how to do it? That's what I just linked, it tells you all about authentication and how to do that.
Ayt i'll read it for the fifth time thanks ! 😄 The solution#1 is taken directly from the doc
Guess I don't understand the issue then. Because you're calling session_start() in a controller, which is really late in the lifecycle of a request, and this would already be taken care off by Laravel in the web.php routes through the default web middleware group
In the second solution which I tried after the Laravel built in one didn't work yes, it was a desperate attempt to make something work... What I don't understand is why I get the same result with the built in login system of Laravel
I followed:
https://laravel.com/docs/11.x/authentication#protecting-routes:~:text=protecting routes.-,Protecting Routes,-Route middleware can
And this
https://laravel.com/docs/11.x/authentication#protecting-routes:~:text=limiting documentation.-,Manually Authenticating Users,-You are not
But I get exactly the same result, when I enter the right password I get redirected to the login view.
Plus it wont help me "make it by myself instead of using Laravel Libs and built in login components". I simply don't understand why the *** session var won't stay