views:

23

answers:

1

It is written in the documentation that it is enough to include the Zend_Form_Element_Hash element in a form and that CSRF protection is handled automatically by Zend_Form.

I have added the token like this:

$token = new Zend_Form_Element_Hash('security_token_against_csrf');
$token->setSalt($this->_helper->randomString());
$form->addElement($token);

But after submitting any form with such token I get this error:

The two given tokens do not match

Do I need to use any method during the form validation as well?

+2  A: 

Have you tried it by setting the salt to be some well-defined (or at least repeatable) string?

When Zend_Form validates, it needs to recreate the hash from session data then compare to what's submitted, and if it's applying a different salt, that would cause tokens to not match.

Dan G
I will try that tomorrow. Thanks.
Richard Knop