为什么在提交时我的表单上存在CSRF令牌错误?

时间:2019-07-29 07:17:31

标签: php html laravel

我的令牌有问题...

TokenMismatchException in VerifyCsrfToken.php line 67:

我的文件中有多种表单,但这是有问题的表单:

<!-- Modal -->


       {!! Form::open(['class' => 'sky-form sky-changes-3','url' => 'message-send','id' => 'sky-form3', 'style' => 'border:none'])  !!}

                                        <input type="hidden" name="contact_user_id" value="{{ $career_solution->user->id }}">
                                        <input type="hidden" name="url" value="{{ url('view-career-solutions') }}/{{ $career_solution->id }}_{{ Slugify::slugify($career_solution->subject) }}">

                                        <fieldset>

                                            <section>
                                                <label class="label">Subject</label>
                                                <label class="input">
                                                    <i class="icon-append fa fa-tag"></i>
                                                    <input type="text" name="subject" id="subject">
                                                </label>
                                            </section>

                                            <section>
                                                <label class="label">Message</label>
                                                <label class="textarea">
                                                    <i class="icon-append fa fa-comment"></i>
                                                    <textarea rows="4" name="message" id="message"></textarea>
                                                </label>
                                            </section>
                                        </fieldset>



      </div>
      <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-dismiss="modal" style="bottom: 2px;position: relative">Close</button>
                                    <button type="submit" class="btn-u btn-u-primary">Send message</button>
                                </div>
                            </div>
                            {!! Form::close() !!}  </div>


在我的代码上方,我有以下几行:

@extends('layout.template')

@section('content')


<input type="hidden" name="_token" value="{{ csrf_token() }}"> 


@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif

这是怎么回事?也许我应该在每种表单上手动添加CSRF令牌吗?我尝试添加@csrf{!! csrf_field() !!},但是我遇到了同样的错误...

4 个答案:

答案 0 :(得分:1)

以下输入必须始终在FORM

<input type="hidden" name="_token" value="{{ csrf_token() }}">

答案 1 :(得分:0)

CSRF令牌应添加到form内。

您已将其添加到表单上方,提交表单时显然不会传递给服务器。

希望有帮助!

答案 2 :(得分:0)

<input type="hidden" name="_token" value="{{ csrf_token() }}"> 

只需将其放入表格中即可。

赞:

<!-- Modal -->


{!! Form::open(['class' => 'sky-form sky-changes-3','url' => 'message-send','id' => 'sky-form3', 'style' => 'border:none'])  !!}

<input type="hidden" name="_token" value="{{ csrf_token() }}">

<input type="hidden" name="contact_user_id" value="{{ $career_solution->user->id }}">
<input type="hidden" name="url" value="{{ url('view-career-solutions') }}/{{ $career_solution->id }}_{{ Slugify::slugify($career_solution->subject) }}">

<fieldset>

    <section>
        <label class="label">Subject</label>
        <label class="input">
            <i class="icon-append fa fa-tag"></i>
            <input type="text" name="subject" id="subject">
        </label>
    </section>

    <section>
        <label class="label">Message</label>
        <label class="textarea">
            <i class="icon-append fa fa-comment"></i>
            <textarea rows="4" name="message" id="message"></textarea>
        </label>
    </section>
</fieldset>



</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" style="bottom: 2px;position: relative">Close</button>
<button type="submit" class="btn-u btn-u-primary">Send message</button>
</div>
</div>
{!! Form::close() !!}  </div>

答案 3 :(得分:0)

每次在应用程序中定义HTML表单时,都应在表单中包含一个隐藏的CSRF令牌字段,以便CSRF保护中间件可以验证请求。

<form method="POST" action="/profile">
    @csrf      //Include inside the form
    ...
</form>