#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Protecting Against CSRF Attacks

Cross-Site Request Forgery (CSRF) tricks a logged-in user's browser into submitting a request they never intended to make. Laravel guards against this automatically for any state-changing request.

The CSRF Token

Every session gets a unique CSRF token, and Laravel checks that token on every POST, PUT, PATCH, and DELETE request submitted through a form.

<form method="POST" action="/profile">
    @csrf
    ...
</form>

The @csrf directive outputs a hidden input containing the token, which Laravel's VerifyCsrfToken middleware then validates automatically.

Handling AJAX Requests

<meta name="csrf-token" content="{{ csrf_token() }}">
axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').content;

Excluding Routes

Certain routes, like webhook endpoints from external services, may need to bypass CSRF checks. These can be listed in the $except property of VerifyCsrfToken.

CSRF protection covers request forgery, but keeping user accounts safe also depends on how passwords themselves are stored — which is the next topic.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course