Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter csrf protection error:

I Am using CI 2. I have enabled CSRF protection in the config file:

$config['csrf_protection']   =  TRUE;
$config['csrf_token_name']   =  'sitename';
$config['csrf_cookie_name']  =  'sitename';
$config['csrf_expire']       =  7200; 

I am using Codeigniter forms only. Sometimes I get the error “you are not authorized to perform this action” wile submitting forms or login.

If I refresh and try again then every thing works fine. Why is this happening?

like image 305
krishnakumar kk Avatar asked Apr 17 '26 09:04

krishnakumar kk


2 Answers

You can make a JS confirm box triggering upon the cookie expiring time, asking the user to extend his session. I think that's the most elegant solution you can get if you want to keep using the CSRF.

like image 106
Shomz Avatar answered Apr 18 '26 23:04

Shomz


Well this happens because your CSRF token expires, its the proper process, increase the CSRF token expiry time from 7200 seconds to something more relevant to your needs (7200 / 60 / 60 = 2 hours).

Read up a bit more on what CSRF does for you here:
http://en.wikipedia.org/wiki/Cross-site_request_forgery

like image 21
Jakub Avatar answered Apr 18 '26 23:04

Jakub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!