How to use Zend Framework Form Hash (token) with AJAX

前端 未结 6 767

I have included Zend_Form_Element_Hash into a form multiplecheckbox form. I have jQuery set to fire off an AJAX request when a checkbox is clicked, I pass the token with thi

6条回答
  •  长情又很酷
    2020-12-28 09:22

    I finally abandoned using Zend_Form_Element_Hash and just created a token manually, registered it with Zend_Session and then checked it upon submission.

    form.php

    $myNamespace = new Zend_Session_Namespace('authtoken');
    $myNamespace->setExpirationSeconds(900);
    $myNamespace->authtoken = $hash = md5(uniqid(rand(),1));
    $auth = new Zend_Form_Element_Hidden('authtoken');
    $auth->setValue($hash)
         ->setRequired('true')
         ->removeDecorator('HtmlTag')
         ->removeDecorator('Label');    
    

    controller.php

    $mysession = new Zend_Session_Namespace('authtoken');
    $hash = $mysession->authtoken;
    if($hash == $data['authtoken']){
        print "success";
    } else {
        print "you fail";
    }
    

    This seems to work and still keeps things relatively sane and secure. I'd still rather use the Hash element, but I can't seem to make it work with AJAX.

    Thanks all.

提交回复
热议问题