CodeIgniter 3 shows 'The action you have requested is not allowed.'
The action you have requested is not allowed.Short answer
With csrf_protection on, CodeIgniter 3 compares the POST token with the CSRF cookie and returns this 403 when either is missing or they differ. Add the hidden field to the form, resend the new token after every Ajax post, or exclude real API URIs.
Fix it fast
1Find out which cause you have
Check the config first. If csrf_protection is TRUE, every POST must carry the token that matches the cookie.
grep -n "csrf_" application/config/config.php2Match what you see, then jump to its fix
- 1
A form written in plain HTML fails, while forms made with form_open() workThe form does not send the token fieldFix - 2
The first POST works; a second POST from the same page, a second tab or Back failsThe token is regenerated on every submissionFix - 3
Webhooks or mobile API calls to a POST route fail with the 403An external or API route is checked as if it were a browser formFix - 4
Even a fresh form fails, and the browser shows no csrf cookieThe CSRF cookie is never stored by the browserFix
- 1
What does the error look like?
The action you have requested is not allowed.403 Forbidden
Cause 1 of 4
The form does not send the token field
1Check
form_open() adds the hidden CSRF field for you. A hand-written form does not.
grep -rn '<form' application/views | grep -v form_open2Fix
Use form_open(), or add the field yourself with the token name and hash helpers.
<form method="post" action="/save">
<input type="hidden"
name="<?= $this->security->get_csrf_token_name(); ?>"
value="<?= $this->security->get_csrf_hash(); ?>">
</form>3Verify
Submit the form; the save runs and no 403 appears.
curl -i -c jar.txt http://localhost/formConfirmed by raw.githubusercontent.com
Cause 2 of 4
The token is regenerated on every submission
1Check
csrf_regenerate is TRUE by default, so each POST invalidates the old token. The docs name back/forward navigation, multiple tabs and asynchronous actions as the problem cases.
grep -n "csrf_regenerate" application/config/config.php2Fix
For Ajax, take the new token from each response and use it on the next post. Or set csrf_regenerate to FALSE if you accept a token that lasts as long as the cookie.
$config['csrf_regenerate'] = FALSE;3Verify
Post twice from the same page without reloading.
# two successful posts in a rowConfirmed by raw.githubusercontent.com
Cause 3 of 4
An external or API route is checked as if it were a browser form
1Check
A POST is validated unless its URI matches csrf_exclude_uris. These callers have no CSRF cookie.
grep -n "csrf_exclude_uris" application/config/config.php2Fix
List only those routes; regular expressions work and matching ignores case.
$config['csrf_exclude_uris'] = array('api/webhook/.+');3Verify
Post to the route without a token; it is accepted by your own authentication.
curl -i -X POST http://localhost/api/webhook/testConfirmed by raw.githubusercontent.com
Cause 4 of 4
The CSRF cookie is never stored by the browser
1Check
With cookie_secure TRUE the cookie is only sent over HTTPS: the framework returns without setting it on a plain HTTP request. The check needs both the POST value and the cookie.
grep -n -E "cookie_secure|cookie_domain|cookie_prefix" application/config/config.php2Fix
Serve the site over HTTPS, or set cookie_secure to FALSE in development.
$config['cookie_secure'] = FALSE; // development only3Verify
Load the form and see the csrf cookie in the browser, then submit.
curl -i http://localhost/form | grep -i set-cookieConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Build forms with form_open() so the field is always present.
- Keep csrf_exclude_uris short and specific, and protect those routes another way.
- Test Ajax pages with two posts in a row, not one.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
jQuery says it cannot use the in operator on a response from PHP
Uncaught TypeError: Cannot use 'in' operator to search for '324' in
The in operator only works on objects, and jQuery.each() uses it.
3 causes, each with a fixWeb apps
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
The text your script returned is not JSON.
4 causes, each with a fixWeb apps
WordPress media upload fails with "Unexpected response from the server"
413 Request Entity Too Large
WordPress shows this message when the upload request did not come back as the JSON it expects.
3 causes, each with a fixWeb apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fix