{"id":13409400,"url":"https://github.com/gilbitron/EasyCSRF","last_synced_at":"2025-03-14T14:31:13.892Z","repository":{"id":27279332,"uuid":"30752628","full_name":"gilbitron/EasyCSRF","owner":"gilbitron","description":"A simple, standalone CSRF protection library","archived":false,"fork":false,"pushed_at":"2021-12-27T14:31:46.000Z","size":26,"stargazers_count":91,"open_issues_count":2,"forks_count":31,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-15T15:39:25.284Z","etag":null,"topics":["csrf","csrf-protection","php"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/gilbitron/easycsrf","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gilbitron.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["gilbitron"]}},"created_at":"2015-02-13T10:56:39.000Z","updated_at":"2024-03-14T10:33:23.000Z","dependencies_parsed_at":"2022-07-25T17:22:13.117Z","dependency_job_id":null,"html_url":"https://github.com/gilbitron/EasyCSRF","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbitron%2FEasyCSRF","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbitron%2FEasyCSRF/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbitron%2FEasyCSRF/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbitron%2FEasyCSRF/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gilbitron","download_url":"https://codeload.github.com/gilbitron/EasyCSRF/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243593363,"owners_count":20316173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csrf","csrf-protection","php"],"created_at":"2024-07-30T20:01:00.474Z","updated_at":"2025-03-14T14:31:13.575Z","avatar_url":"https://github.com/gilbitron.png","language":"PHP","readme":"[![Build Status](https://travis-ci.org/gilbitron/EasyCSRF.svg?branch=master)](https://travis-ci.org/gilbitron/EasyCSRF) [![Packagist Downloads](https://img.shields.io/packagist/dm/gilbitron/easycsrf)](https://packagist.org/packages/gilbitron/easycsrf) ![PHP version](https://img.shields.io/travis/php-v/gilbitron/easycsrf/master) ![License](https://img.shields.io/github/license/gilbitron/easycsrf)\n\n# EasyCSRF\nEasyCSRF is a simple, standalone CSRF protection library written in PHP. It can be used to\nprotect your forms from [Cross Site Request Forgery](http://en.wikipedia.org/wiki/Cross-site_request_forgery) attacks.\n\n## Requirements\n\n* PHP 7.3+\n\n## Install\n\nInstall via [composer](https://getcomposer.org):\n\n```\ncomposer require gilbitron/easycsrf\n```\n\nRun `composer install` then use as normal:\n\n```php\nrequire 'vendor/autoload.php';\n\n$sessionProvider = new EasyCSRF\\NativeSessionProvider();\n$easyCSRF = new EasyCSRF\\EasyCSRF($sessionProvider);\n```\n\n## Usage\n\nTo use EasyCSRF first you need to generate a token:\n\n\n```php\n$sessionProvider = new EasyCSRF\\NativeSessionProvider();\n$easyCSRF = new EasyCSRF\\EasyCSRF($sessionProvider);\n\n$token = $easyCSRF-\u003egenerate('my_token');\n```\n\nYou then include this token with any forms you create:\n\n```html\n\u003cform\u003e\n    ...\n    \u003cinput type=\"hidden\" name=\"token\" value=\"\u003c?php echo $token; ?\u003e\"\u003e\n    ...\n\u003c/form\u003e\n```\n\nThen before you do any data processing, you check the token is valid:\n\n```php\nuse EasyCSRF\\Exceptions\\InvalidCsrfTokenException;\n\ntry {\n    $easyCSRF-\u003echeck('my_token', $_POST['token']);\n} catch(InvalidCsrfTokenException $e) {\n    echo $e-\u003egetMessage();\n}\n```\n\n## Token Expiration\n\nYou can set a time limit on tokens by passing a timespan (in seconds) to the\ncheck method. Tokens older than the timespan will not be valid.\n\n```php\n// Example 1 hour expiration\n$easyCSRF-\u003echeck('my_token', $_POST['token'], 60 * 60);\n```\n\n## Reusable Tokens\n\nTokens can be made reusable and not one-time only (useful for ajax-heavy requests).\n\n```php\n// Make token reusable\n$easyCSRF-\u003echeck('my_token', $_POST['token'], null, true);\n```\n\n## Custom SessionProvider\n\nYour app might use a third party library for managing sessions, or you may want to store tokens somewhere other\nthan $_SESSION (as the `NativeSessionProvider` does). In this case you can create a custom `SessionProvider`\nand use that when instantiating EasyCSRF.\n\n```php\n\u003c?php\n\nuse EasyCSRF\\Interfaces\\SessionProvider;\n\nclass CustomSessionProvider implements SessionProvider\n{\n    /**\n     * Get a session value.\n     *\n     * @param string $key\n     * @return mixed\n     */\n    public function get($key)\n    {\n        // Return your stored data\n    }\n\n    /**\n     * Set a session value.\n     *\n     * @param string $key\n     * @param mixed $value\n     * @return void\n     */\n    public function set($key, $value)\n    {\n        // Store your data\n    }\n\n}\n```\n\n```php\n$sessionProvider = new CustomSessionProvider();\n$easyCSRF = new EasyCSRF\\EasyCSRF($sessionProvider);\n```\n\n## Credits\n\nEasyCSRF was created by [Gilbert Pellegrom](http://gilbert.pellegrom.me) from [Dev7studios](http://dev7studios.co).\nReleased under the MIT license.\n","funding_links":["https://github.com/sponsors/gilbitron"],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilbitron%2FEasyCSRF","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgilbitron%2FEasyCSRF","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilbitron%2FEasyCSRF/lists"}