Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 27 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

Understanding CSRF Protection: Secure Your Laravel Forms

Stop Cross-Site Request Forgery (CSRF) in its tracks. Learn how the @csrf directive works, why it's vital for your forms, and how to manage token expiration.

LaravelSecurityCSRFWeb DevelopmentPHPbackend

Previously in this course, we explored Protecting Routes with Middleware: A Laravel Beginner’s Guide to restrict access to authenticated users. Now that we have a secure authentication layer, we need to ensure that the data being submitted to our application is actually coming from our own forms.

What is CSRF?

Cross-Site Request Forgery (CSRF) is a vulnerability where a malicious website tricks a user's browser into performing an unwanted action on a different website where that user is currently authenticated.

Imagine you are logged into your Task Manager app. A malicious site you visit in another tab could contain a hidden form that sends a POST request to your-app.com/tasks/delete/1. Because your browser automatically includes your session cookies with that request, your app might think you intentionally clicked that delete button.

Laravel prevents this by requiring a secret, unique token for every state-changing request (POST, PUT, PATCH, DELETE). If the token is missing or invalid, Laravel rejects the request.

The @csrf Directive

When you build a form in Laravel, you must include a hidden input field containing a CSRF token. The simplest way to do this is using the Blade @csrf directive.

Open your resources/views/tasks/create.blade.php file and add the directive inside your form:

HTML
style="color:#808080"><style="color:#4EC9B0">form action="/tasks" method="POST">
    @csrf
    
    style="color:#808080"><style="color:#4EC9B0">label for="title">Task Namestyle="color:#808080"></style="color:#4EC9B0">label>
    style="color:#808080"><style="color:#4EC9B0">input type="text" name="title" id="title">

    style="color:#808080"><style="color:#4EC9B0">button type="submit">Create Taskstyle="color:#808080"></style="color:#4EC9B0">button>
style="color:#808080"></style="color:#4EC9B0">form>

When this page renders, Laravel replaces @csrf with an HTML input field that looks like this:

HTML
style="color:#808080"><style="color:#4EC9B0">input type="hidden" name="_token" value="a-long-random-string-of-characters">

When the user submits the form, this token is sent to the server. Laravel’s VerifyCsrfToken middleware (which is active by default on all web routes) compares the token in the request with the token stored in the user's current session. If they match, the request proceeds.

Handling Token Expiration

Sometimes, your users might leave a form open in their browser for a long time. If their session expires, the CSRF token becomes invalid. When they finally click "Submit," they will be greeted by a 419 Page Expired error.

This is a security feature, not a bug. To handle this gracefully:

  1. Keep Sessions Alive: If your application requires long form-filling times, consider extending your session lifetime in config/session.php.
  2. User Experience: You can catch the TokenMismatchException in your app/Exceptions/Handler.php (or via global exception handling) to redirect the user back to the form with a helpful message: "Your session has expired. Please refresh the page and try again."

Hands-on Exercise

Let's secure our Task Manager's "Create Task" form:

  1. Locate your existing create.blade.php file.
  2. Add the @csrf directive inside the <form> tags.
  3. Try submitting the form without the directive. You will see a 419 Page Expired error—this confirms that Laravel's security middleware is working as intended.
  4. Add the directive back and verify that the task saves successfully.

Common Pitfalls

  • Forgetting the Directive: The most common mistake is omitting @csrf in a POST, PUT, or DELETE form. You will always get a 419 error.
  • Using GET for Actions: Never use a GET request to perform actions like deleting or updating data. CSRF protection only applies to state-changing methods. If you use GET for a delete action, you are bypassing this security layer entirely.
  • AJAX Requests: If you are using JavaScript to send data (like Axios or Fetch), you cannot use the @csrf directive. Instead, you must include the token in the request header. Laravel expects the token in the X-CSRF-TOKEN header.

Recap

CSRF is a serious threat, but Laravel makes protection effortless. By using the @csrf directive in every state-changing form, you ensure that only requests originating from your own application are processed. Remember: if you see a 419 Page Expired error, the first thing to check is that your form includes the hidden token.

Up next, we’ll look at how to prevent mass assignment vulnerabilities, ensuring users can only update the database fields we explicitly allow.

Previous lessonProtecting Routes with MiddlewareNext lesson Preventing Mass Assignment
Back to Blog

Similar Posts

LaravelJune 25, 20263 min read

Introduction to Route Model Binding in Laravel

Stop manually querying the database in your controllers. Learn how route model binding automatically injects Eloquent models into your routes.

Read more
LaravelJune 25, 20263 min read

Protecting Routes with Middleware: A Laravel Beginner’s Guide

Learn how to use Laravel middleware to secure your routes. We'll cover applying the auth middleware, protecting route groups, and managing redirects.

Part of the course

Laravel Fundamentals: From Zero to Your First App

beginner · Lesson 27 of 52

  1. 1

    Setting Up the Local Development Environment

    4 min
  2. 2

    Installing Laravel and Exploring Directory Structure

    3 min
  3. 3

    Understanding the .env File and Configuration

    3 min
Read more
LaravelJune 25, 20263 min read

Customizing Validation Error Messages for Better Laravel UX

Learn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.

Read more
  • 4

    The Laravel Application Lifecycle

    4 min
  • 5

    Initializing the Task Manager Project

    3 min
  • 6

    Defining Basic Web Routes

    4 min
  • 7

    Using Route Parameters

    3 min
  • 8

    Creating Your First Controller

    3 min
  • 9

    Returning Responses and Redirects

    3 min
  • 10

    Task Manager: Implementing the Task List Route

    3 min
  • 11

    Introduction to Blade Templating

    3 min
  • 12

    Using Blade Layouts and Sections

    3 min
  • 13

    Implementing Blade Partials

    4 min
  • 14

    Mastering Blade Directives for Loops and Conditionals

    3 min
  • 15

    Task Manager: Building the User Interface

    3 min
  • 16

    Understanding Database Migrations

    3 min
  • 17

    Working with Eloquent Models

    3 min
  • 18

    Performing Basic CRUD Operations

    3 min
  • 19

    Seeding the Database

    3 min
  • 20

    Task Manager: Displaying Real Database Records

    3 min
  • 21

    Capturing User Input from Forms

    4 min
  • 22

    Introduction to Laravel Validation

    3 min
  • 23

    Customizing Validation Error Messages

    3 min
  • 24

    Using Form Requests for Validation

    3 min
  • 25

    Introduction to Authentication

    4 min
  • 26

    Protecting Routes with Middleware

    3 min
  • 27

    Understanding CSRF Protection

    3 min
  • 28

    Preventing Mass Assignment

    3 min
  • 29

    Task Manager: Securing the Application

    3 min
  • 30

    Introduction to Route Model Binding

    3 min
  • 31

    Updating Existing Records

    Coming soon
  • 32

    Deleting Records

    Coming soon
  • 33

    Using Named Routes

    Coming soon
  • 34

    Task Manager: Completing CRUD Functionality

    Coming soon
  • 35

    Introduction to Database Relationships

    Coming soon
  • 36

    Querying Related Data

    Coming soon
  • 37

    Handling File Uploads

    Coming soon
  • 38

    Using Flash Messages for User Feedback

    Coming soon
  • 39

    Task Manager: Adding Status and Priorities

    Coming soon
  • 40

    Introduction to Artisan Commands

    Coming soon
  • 41

    Debugging with Laravel Tinker

    Coming soon
  • 42

    Understanding Service Providers

    Coming soon
  • 43

    Using View Composers

    Coming soon
  • 44

    Task Manager: Refactoring for Clean Code

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Testing Forms and Validation

    Coming soon
  • 47

    Using Database Transactions

    Coming soon
  • 48

    Handling Global Exceptions

    Coming soon
  • 49

    Preparing for Production

    Coming soon
  • 50

    Environment Security Best Practices

    Coming soon
  • 51

    Managing Assets in Production

    Coming soon
  • 52

    Task Manager: Deployment Preparation

    Coming soon
  • View full course