Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working basic example #38

Closed
jordy-u opened this issue Jul 8, 2020 · 0 comments
Closed

Working basic example #38

jordy-u opened this issue Jul 8, 2020 · 0 comments

Comments

@jordy-u
Copy link

jordy-u commented Jul 8, 2020

Thanks for creating this library! Below I shared a quick example of how to use it.
Save the code below as a PHP file, put it on your webserver and run it.

Before running:<br>
1. Open a terminal and navigate to your webserver root directory.<br>
2. Download the library with: "composer require paragonie/anti-csrf".<br>
The package is now installed in the vendor/ folder.<br><br>
<?php
//Don't forget to call this:
session_start();

//Because of $_SERVER['DOCUMENT_ROOT'], you can place this PHP-file anywhere on the webserver.
require $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';

use ParagonIE\ConstantTime\{
    Base64UrlSafe,
    Binary
};
use ParagonIE\AntiCSRF\AntiCSRF;
?>

<form method="POST">
    <?php
    //Generate CSRF token
    $server = $_SERVER;
    $csrft = new AntiCSRF( $server);
    $token_html = $csrft->insertToken('', true);
    ?>
    <input type="submit">
</form><br>

<?php
//Check CSRF token
$csrf = new \ParagonIE\AntiCSRF\AntiCSRF;
if (!empty($_POST)) {
    if ($csrf->validateRequest()) {
        echo 'Valid form';
    } else {
        // Log a CSRF attack attempt
        echo 'Token is invalid';
    }
}
?>

The reason I share this, is because it took me a while to understand how to use the library. So I hope this helped others.
Also, if you are reading this and didn't really use composer before, like me, This tutorial should help you out.

@jordy-u jordy-u closed this as completed Jul 8, 2020
@jordy-u jordy-u changed the title Basic example Working basic example Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant