Search code examples
laravelapachecsrf

How Come My X-CSRF Token And Session One Are Not Generated In The Live Server?


I am trying to upload an application that uses laravel to a live apache server that uses https but I noticed my post requests stopped working in the process and I noticed that my cookies in the request and response lacked the x-csrf token and session one as well to proceed with the request. For my language switch post (supposed to switch languages) this happens and I get a page expired error

//web.php

Route::post('/language/switch', [LanguageController::class,'switchLanguage'])->name('language.switch');

//languageController.php
 
public function switchLanguage(Request $request) {
            $language = $request->language;
            app()->setLocale($language);
            $previousUrl = url()->previous();
            $route = Route::getRoutes()->match(Request::create($previousUrl));
            $routeName = $route->getName();
            $newRouteName = preg_replace('/^(en|fr)\.(.*)$/', $language . '.\2', $routeName);
            return redirect()->route($newRouteName);
        }

//navbar.blade.php

<li class="nav-item dropdown">
                  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                     {{app()->getLocale() == 'en' ? 'English' : 'Français'}}
                   </a>
                   <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                     <form action="{{route('language.switch')}}" method="POST">
                       @csrf
                       <input type="hidden" name="language" value="{{app()->getLocale() == 'en' ? 'fr' : 'en'}}">
                       <button class="dropdown-item" type="submit">{{app()->getLocale() == 'en' ? 'Français' : 'English'}}</button>
                     </form>
                   </div>
                </li>

It is not just the post that gets a page expired error the others do too and I have @csrf on all of them. I tried using a hidden input as well to no avail how can I fix the page expired error?


Solution

  • I used another web host and that seems to have fixed the problem I had.