You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Simple CSRF control class with PHP. With this php class you can generate and validate tokens that are disposable or refreshed on every page refresh. The generated tokens are encrypted with openssl for extra security, so you need the openssl extension on your php server.
It compares the token you have printed on your forms with the token registered in the session and checks its accuracy. Create a _csrf entry in your forms and print the value generated by the class using the Get() method.
Check() Example
$csrf->Check($token);
Check() Result
true/false
Reset()
Use this method to reset and regenerate the token after verifying the token. If you want, you can increase the security a little more by creating a new token every time the page is refreshed.
Reset() Example
$csrf->Reset();
Reset() Result
true/false
Example Form Usage and Controls
<?phpsession_start();// Start sessions.// Include the CSRF Class in your file.// Configure the class.$csrf =newCsrf(['key' =>'SuperKey','secret' =>'SuperSecret' ]);if($_POST){$firstname =$_POST['firstname'];$_csrf =$_POST['_csrf'];// We get the _csrf value from the form.// We verify the token from the form with the Check() method.if($csrf->Check($_csrf)){$result ="Token is correct";$csrf->Reset();// We reset the token. }else{$result ="Token is not correct";$csrf->Reset();// We reset the token. } }?><form method="POST" action="post.php"> <input type="text" name="firstname"><br> <input type="text" name="_csrf" value="<?=$csrf->Get();?>"><br> <button type="submit">Submit</button></form>