My CodeIgniter CSRF + Form Helpers |
[eluser]Unknown[/eluser]
Hi all, I've created a CSRF & Form (Extended from CI Form) helpers that aim to tackle the CSRF exploit, and would like to contribute it to anybody who would be interested. Save the following code into: system/application/helpers/csrf_helper.php Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Save the following code into: system/application/helpers/MY_form_helper.php Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Edit the file: system/application/config/autoload.php so as to get CI to autoload the necessary helpers. Code: /* To protect your form against CSRF, you just need to add: Code: <?=form_csrf();?> Example: Code: <form method="post"> and you are done! Whenever the form get submitted, the CSRF Helper will validate the token. You can find a more complete post of my codes here.
[eluser]fdog[/eluser]
Thanks xciden! I've been needing a helper like this for a while.
[eluser]kenjis[/eluser]
session_unregister() is deprecated. It's better not to use it. http://php.net/session_unregister This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
[eluser]echoDreamz[/eluser]
Deprecated + the fact that CodeIgniter does not use PHP's native sessions.
[eluser]Linh Pham[/eluser]
Hi there, i have used this helper to prevent duplicate form submition, i tried 3 click times on the submit button and it does not work, so how to use this? submit to insert the record into the database and with 3 click times and had 3 same records, so that it's not good for my aim, i just want only record will be insert into db
[eluser]skunkbad[/eluser]
[quote author="Linh Pham" date="1339566280"]Hi there, i have used this helper to prevent duplicate form submition, i tried 3 click times on the submit button and it does not work, so how to use this? submit to insert the record into the database and with 3 click times and had 3 same records, so that it's not good for my aim, i just want only record will be insert into db[/quote] I made my own CSRF library, and you can use it too if you want: https://bitbucket.org/skunkbad/community...s/csrf.php Use is really easy, especially if you use the MY_form_helper: https://bitbucket.org/skunkbad/community...helper.php All you have to do is load the library in your controller: Code: $this->load->library('csrf'); Then, to check for valid form submission: Code: if( $this->csrf->token_match ) CodeIgniter has it's own CSRF protection now, but this library can be used in older version of CI, or in combination with CI's CSRF protection. It's been fully tested and I use it every day.
[eluser]Linh Pham[/eluser]
[quote author="skunkbad" date="1339568755"][quote author="Linh Pham" date="1339566280"]Hi there, i have used this helper to prevent duplicate form submition, i tried 3 click times on the submit button and it does not work, so how to use this? submit to insert the record into the database and with 3 click times and had 3 same records, so that it's not good for my aim, i just want only record will be insert into db[/quote] I made my own CSRF library, and you can use it too if you want: https://bitbucket.org/skunkbad/community...s/csrf.php Use is really easy, especially if you use the MY_form_helper: https://bitbucket.org/skunkbad/community...helper.php All you have to do is load the library in your controller: Code: $this->load->library('csrf'); Then, to check for valid form submission: Code: if( $this->csrf->token_match ) CodeIgniter has it's own CSRF protection now, but this library can be used in older version of CI, or in combination with CI's CSRF protection. It's been fully tested and I use it every day.[/quote] Thank you so much, it does work fine on my app |
Welcome Guest, Not a member yet? Register Sign In |