Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

antoinebrune's avatar

Advice on login form with sessions variables

Hello,

I need some advice. I have a website I made 15 years ago in PHP to bet on the Tour de France with some friends (we are like 15 to play each year, so not a big website) :) I am now learning laravel, and would like to convert this site using laravel in order to learn Laravel. I already started, inspiring myself from the different videos on Laracasts, and especially the example of Tweety. On my current website, the code is the same if you play for the Tour de France, or the Giro d'Italia or the Vuelta, and it's the same if you play for 2020, 2019, 2018 etc... I only added on some pages some conditions depending on the race (like css colors etc...). To solve this I had added in every table one column called "race" and one column called "year". When login in, on the login form I had added a dropdown with the race and the year and I was saving this on session's variables. I was then appending every query I had to do with these 2 parameters (where year=$_SESSION['year'] and race=$_SESSION['race']. Main issue is that on my user table, I add to duplicate users for every race, every year, since my user table contained also these 2 extra columns.

How would you do this in Laravel? I was think of creating a table containing all my races and years, then a pivot table containing the user_id and the id of this table. But then how during login, can I track what the user chose as race and year, and maintain it during all the session? Shall I also work with some sessions?

I am a bit lost here...

Thanks upfront

0 likes
5 replies
vladv's avatar

Hi,

For simplicity and not breaking other stuff :) I would keep the old logic ...

I suppose you are using the Laravel standard Auth, in order to make your logic work try this:

Auth\LoginController.php add

    /**
     * Send the response after the user was authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

	Session::put('year', request()->year');

        return $this->authenticated($request, $this->guard()->user())
                ?: redirect()->intended($this->redirectPath());
    }

and then you can Session::get('year) anywhere after login. (I didn't test ;) )

The wise way would be to login without selecting the year/race/etc ... and let the user choose after, then save it in your URL as parks would be more friendly I guess. And of course, the DB structure would be much cleaner with a pivot table ie. user_race; user_id, race_id, year

Good luck

antoinebrune's avatar

Thanks a lot ! That seems indeed to be the way to work like I did with sessions.

Regarding your second point, I'm interested :) So I would log my user in, come to a page where he would choose for example: Tour de France 2020, and be redirected to some kind of mywebsite/tourdefrance/2020 ? Correct? From there I could reuse the information in the URL for my queries ?

Thanks upfront.

antoinebrune's avatar

Thanks :) I'm ready to continue now :) Maybe last question, how can I make sure only the users who have an entry in my pivot table can access a certain route? Basically, how do I make sure, that only the users that are "subscribed" for the Tour de France 2020, can access: mywebsite/tour-de-france/2020/home (and sub-sequentially mywebsite/tour-de-france/2010/stages etc...) and not the one who participated to the 2019 version but didn't want to participate to the 2020 version.

vladv's avatar

That's another topic ;) If the relationship it's 1 to 1 (category - user) then u could add the category_id directly in the users' table and create a middleware (https://laravel.com/docs/7.x/middleware) where u store the access rules. Otherwise if the relationship it's 1 too many you need another pivot table.

Please or to participate in this conversation.