如何在zend框架中创建验证码?

时间:2011-06-24 07:21:14

标签: javascript zend-framework captcha

如何在Zend Framework上运行的应用程序中创建CAPTCHA响应/质询?有没有内置的库?

2 个答案:

答案 0 :(得分:3)

查看Zend Framework的直接链接 - http://framework.zend.com/manual/en/zend.captcha.introduction.html

生成:

//generates an instance of Zend_Captcha
//returns ID of captcha session
function generateCaptcha() {
    $captcha = new Zend_Captcha_Image();

    $captcha->setTimeout(’300′)
    ->setWordLen(’6′)
    ->setHeight(’80′)
    ->setFont(‘/path/to/your/fontFile.ttf’)
    ->setImgDir(‘/path/to/your/image/captchaDirectory’);

    $captcha->generate();    //command to generate session + create image

    return $captcha->getId();   //returns the ID given to session & image
}   //end function generateCaptcha

验证

//validates captcha response
function validateCaptcha($captcha) {
    $captchaId = $captcha[‘id’];
    $captchaInput = $captcha[‘input’];

    $captchaSession = new Zend_Session_Namespace(‘Zend_Form_Captcha_’ . $captchaId);
    $captchaIterator = $captchaSession->getIterator();
    $captchaWord = $captchaIterator[‘word’];

    if( $catchaWord ) {
        if( $captchaInput != $captchaWord ){
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }

}

示例:

http://mnshankar.wordpress.com/2009/08/13/understanding-zend-captcha-zend_captcha_image/

http://krmaurya.com/13/hello-world/

答案 1 :(得分:0)

可以从以下stackoverflow.com问题中获得可能的答案: