Warning: Undefined variable $post in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/functions.php on line 12

Warning: Attempt to read property "ID" on null in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/functions.php on line 12

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33

【Laravelメモ】419のHTTPエラーが出た時の対処法

記事内に商品プロモーションを含む場合があります


Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33

この記事では、Laravelで419エラーが出た場合の対処法について紹介します。

そもそも419エラーってどんなエラー?

419エラーは実はHTTPステータスコードに記載のないエラーコードなので、定義されていないステータスコードになります。


参考
HTTPステータスコードWikipedia

419エラーの原因は?

419エラーが定義されていないステータスなら、原因わからんやん・・・。

と思うかもしれませんが、大丈夫です!

LaravelではCSRF対策としてCSRFトークンを使用することができますが、これを使用していない場合に419エラーが出るようになっています。

なので、CSRF対策の仕組みを使えば解決です。


参考
クロスサイトリクエストフォージェリ(CSRF)とはTREND MICRO

CSRFトークンを使用するには

CSRF対策と聞くと、難しいように聞こえるかもしれませんが、超簡単です。

Laravelではform内に「 @csrf 」と書くだけでおしまいです!

<form action="/login" method="post">
    //csrf対策
    @csrf

    <input type='text' name="name" value="{{ old('name') }}">
    <input type='password' name="password" value="{{ old('password') }}">
    <input type='submit'  value="ログイン">

</form>

これで、419エラーが解消されます。

注意

Laravelのバージョンが5.5以前の場合、「@csrf」ではなく「 {{ csrf_field() }} 」と書きます。

なので、formを使う場合は「@csrf」をおまじないとして書くこと忘れないようにしましょう。

おすすめのLaravelの書籍

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です

CAPTCHA



Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33