简体   繁体   English

PHP表单中的Laravel CSRF令牌

[英]Laravel csrf token within PHP form

I created a little helper function for accepting friend requests. 我创建了一个小助手功能来接受朋友的请求。 This function lies within a PHP file (obviously) and looks like this: 该函数位于一个PHP文件中(很明显),看起来像这样:

(Only the relevant part) (仅相关部分)

foreach($friendrequests as $request){
    $username = DB::table('users')->where('id', $request->sender_id)->value('name');
    $notify .= '<li>';
    $notify .= '<strong><a href="/profile/'.$username.'">'.$username.'</a></strong><br>möchte dein Freund sein';
    $notify .= '<form action="/friend/request/accept/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="csrf_field();"><button type="submit">Akzeptieren</button></form>';
    $notify .= '<form action="/friend/request/deny/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="csrf_field();"><button type="submit">Ablehnen</button></form>';
    $notify .= '</li>';
}

I know it's kind of messy. 我知道这有点乱。 I'm fairly new to Laravel. 我是Laravel的新手。

Anyway, there are two forms. 无论如何,有两种形式。 One for accepting and one for denying the request. 一种用于接受,另一种用于拒绝请求。 Now the thing I'm struggling with is the csrf token. 现在,我正在努力的是csrf令牌。

How do I implement this within the PHP helper file? 如何在PHP帮助程序文件中实现此功能? I know how to use them in the blade templates, but I can't seem to make it work within the helper function. 我知道如何在刀片模板中使用它们,但是我似乎无法使其在助手功能中起作用。

Try to add _token hidden element to your code as below. 尝试将_token隐藏元素添加到您的代码中,如下所示。 You can also use csrf_token() helper function to add the form token inside forms. 您还可以使用csrf_token() 帮助函数在表单内添加表单令牌。

foreach($friendrequests as $request){
        $username = DB::table('users')->where('id', $request->sender_id)->value('name');
        $notify .= '<li>';
        $notify .= '<strong><a href="/profile/'.$username.'">'.$username.'</a></strong><br>möchte dein Freund sein';
        $notify .= '<form action="/friend/request/accept/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="'.Session::token().'"><button type="submit">Akzeptieren</button></form>';
        $notify .= '<form action="/friend/request/deny/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="'.Session::token().'"><button type="submit">Ablehnen</button></form>';
        $notify .= '</li>';
    }

You have added the fields, but you need to concatenate the csrf_token() value to your string. 您已经添加了字段,但是需要将csrf_token()值连接到字符串。 Right now, it will literaly print csrf_token as value. 现在,它将按csrf_token意义将csrf_token打印为值。

Try this: 尝试这个:

$notify .= '<form action="/friend/request/accept/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="' . csrf_token() . '"><button type="submit">Akzeptieren</button></form>';

Also, the csrf_field() function will echo an input field with the tokens value to the current request, csrf_token() will display only the token value. 此外, csrf_field()函数将回显带有令牌值的输入字段到当前请求, csrf_token()将仅显示令牌值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM