Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.3 - TokenMismatchException on every post request

Tags:

laravel

I was working on my project normally and when i hit refresh to see a change that i just made in one of the html pages, the page showed a text on top left "Redirecting to: http://localhost:8888/xxx ". It redirected me to the login page. When i clicked login, the 'TokenMismatchException' error showed: enter image description here

The login form has the hidden input _token , header has it too. As i said everything was fine. I have been working on this project for 2 months. Is this related to files permissions ?

Here is the login form.

<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">

                    {{ csrf_field() }}

                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                        <div class="col-md-7">
                            <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

                            @if ($errors->has('email'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Password</label>

                        <div class="col-md-7">
                            <input id="password" type="password" class="form-control" name="password" required>

                            @if ($errors->has('password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                     <div class="form-group">
                        <div class="col-md-8 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Login
                            </button>
                            <a class="btn btn-link" href="{{ url('/password/reset') }}">
                                Forgot Your Password?
                            </a>
                        </div>
                    </div>


                </form>

Thanks :)

like image 518
Antonio Gocaj Avatar asked Dec 02 '25 09:12

Antonio Gocaj


2 Answers

After i installed a fresh laravel copy i started to paste my old files to see where would regenerate the mismatch token problem. It went down to the routes/web.php file. Something there was causing the error. It was a space before the opening tag of php on routes/web.php file. Something that did not caught my eye. Like that the project was working fine but suddenly did not anymore.

As i read , the space is considered an output. Check this explanation : https://stackoverflow.com/a/4345822/6634389

like image 136
Antonio Gocaj Avatar answered Dec 04 '25 01:12

Antonio Gocaj


Why don't you use {{ csrf_field() }} or try with that?

like image 26
Tim Avatar answered Dec 04 '25 00:12

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!