2017 © Pedro Peláez
 

library csrf-protection

Protect your web app from malicious requests by using CSRF protection tokens in links and forms

image

internetpixels/csrf-protection

Protect your web app from malicious requests by using CSRF protection tokens in links and forms

  • Wednesday, June 13, 2018
  • by Petervw
  • Repository
  • 1 Watchers
  • 2 Stars
  • 73 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

CSRF Protection

Protect your web app from malicious requests by using CSRF protection tokens in links and forms. This CSRF protection library does not use sessions, files, memory storage or databases., (*1)

License Build Status Maintainability, (*2)

Basic examples

In the examples below you'll find the 3 most important methods: - TokenManager::create(); - TokenManager::createHtmlField(); - TokenManager::validate();, (*3)

The full namespace is InternetPixels\CSRFProtection\TokenManager., (*4)

Setup the TokenManager

You have to set some settings for a secure TokenManager. Please overwrite the salt by your own one., (*5)

<?php
TokenManager::setSalt('P*17OJznMttaR#Zzwi4YhAY!H7hPGUCd', 'ERGirehgr4893ur43tjrg98rut98ueowifj');
TokenManager::setUserId(7);
TokenManager::setSessionToken('session_token');

Create a safe form

The TokenManager can improve the security, (*6)

<form action="/your/page" method="post">
    <?= TokenManager::createHtmlField('my_form') ?>
</form>

When a user wants to delete an item in your application, you want to be sure that the request is valid. Create a token and add it in the link to your delete page., (*7)

<a href="/posts/delete/1?token=<?= TokenManager::create('delete_post') ?>">Delete Post</a>

In the delete action you want to validate the token with the TokenManager::validate() method., (*8)

Validate the user input

Once a user is posting the form data to your server(s), you'll first need to validate the given token. By default, the field name used is _token., (*9)

<?php

if( filter_has_var(INPUT_POST, '_token') ) {
    if( TokenManager::validate('my_form', filter_input(INPUT_POST, '_token')) ) {
        // valid token
    }
    else {
        // invalid token
    }
}

The Versions

13/06 2018

dev-master

9999999-dev

Protect your web app from malicious requests by using CSRF protection tokens in links and forms

  Sources   Download

The Requires

  • php >=7.1

 

The Development Requires

by Peter van Wilderen

12/06 2018

0.1.0

0.1.0.0

Protect your web app from malicious requests by using CSRF protection tokens in links and forms

  Sources   Download

The Requires

  • php >=7.1

 

The Development Requires

by Peter van Wilderen