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

Talbergs's avatar

1/1 TokenMismatchException in VerifyCsrfToken.php line 68:

Hello i just started learn PHP and Laravel I created simple html post page and simple Route to see entered values. And now i have Error with Token. I know i must somehow write {{ csrf_token() }} but where and how i dont known.

HTML Code:

<body>

    <form action="/comments" method="POST">

        Name:
        <input type="text" name="name" /><br />
        Comment:<br />
        <textarea name="text"></textarea>

        <!--<input type="hidden" name="_method" value="PUT">-->
        <br />
        <input type="submit" value="Pievienot"/>
    </form>
</body>

Simple ROUTE:


Route::post('/comments', function ()
{
    print_r($_POST);
});
0 likes
4 replies
Snapey's avatar

it's all there in the docs...

have a look at laravel from scratch series on this site... it's free.

you need {{ csrf_field() }} somewhere in your form

but this will only work if your html code is in a blade file.

2 likes
Arpit's avatar

Laravel protects you from csrf attacks out of the box so you need to include {{ csrf_field() }}

<form action="/comments" method="POST">
{{ csrf_field() }}

    Name:
    <input type="text" name="name" /><br />
    Comment:<br />
    <textarea name="text"></textarea>

    <!--<input type="hidden" name="_method" value="PUT">-->
    <br />
    <input type="submit" value="Pievienot"/>
</form>

As @Snapey said have a look at laravel from scratch series on this site, you are not gonna regret it.

2 likes
Talbergs's avatar
Talbergs
OP
Best Answer
Level 1

Thanks Alot. I Finished with my question. Solution: I created form.blade.php View and inserted the hidden" value="{{ csrf_token() } in my form.

<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta name="" content="">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="/comments" method="POST">
    <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
    Name::
    <input type="text" name="name" /><br />
    Comments:<br />
    <textarea name="text"></textarea>

    <!--<input type="hidden" name="_method" value="PUT">-->
    <br />
    <input type="submit" value="Pievienot"/>
</form>
</body>
</html>

And aCreated a New Route form to View it:

Routes:

Route::get('/comment', function () {
    return view('form');
});
Route::post('/comments', function ()
{
    print_r($_POST);
});
Snapey's avatar

well look at you, creating forms and everything...

Please or to participate in this conversation.