Laravel Helper Function: csrf_token

November 20, 2016 —John Koster

csrf_token()

The csrf_token (cross-site request forgery token) function is a shortcut for retrieving the CSRF token from the session storage.

The following is an example of what the csrf_token function may output:

1<?php
2 
3// WGuFrjvm7keNGqDhhW8jVnyC3W6zUv9w4mPexw9N
4csrf_token();
CSRF Token

The CSRF token is stored in the session and is a random string 40 characters in length. It is generated using the str_random(40) function call.

The csrf_token function can also be used when generating HTML forms:

1<!DOCTYPE html>
2<html>
3<head>
4 <title>CSRF Token Form Sample</title>
5</head>
6<body>
7 <form>
8 <input type="hidden" name="_token"
9 value="<?php echo csrf_token(); ?>">
10 
11 <!-- Other form inputs here -->
12 </form>
13</body>
14</html>

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.