Giter VIP home page Giter VIP logo

csrf-class's Introduction

CSRF Class

This is a simple yet effective class to enable you to protect your forms from CSRF attacks.

What is CSRF? A CSRF attack or Cross Site Request Forgery as it's known is where a malicious user takes advantage of your logged in state. For example, say you used a get request to logout a user, like so:

http://mysite.com/user/logout

This is a perfectly valid URL, however if this user was also logged in on another site, they could take advantage of this by placing this in an image tag:

<img src="http://mysite.com/user/logout" />

This would cause the user to be logged out on your site. This is an annoying, but not too serious example.

To protect from this we use what's called a security token which ensures that the request did come from our site. You could use it in a get request like so:

http://mysite.com/user/logout?hash=6f792794d27d157fda64bc51f296e4f3

This would prevent that image tag logging the user out as the security token wouldn't be present. However, there can be problems doing it this way, so your better off logging a user out via a post request in a form.

Usage

If you look in the form_demo.php file, you will see an example of how the class can be used.

Step 1: Echo Token::display(); in your form.

Step 2: Check for a post request and whether the security token is valid. If you want you can also check if it's recent:

if (!Token::isValid() OR !Token::isRecent())
{ 
   $errors[] = 'Invalid Security Token'; 
   // Stop further processing.
}

Step 3: That's it!

It's literally as easy as 1..2..3!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.