PHP 会话问题-- captcha/Joomla

发布于 2024-09-25 03:29:08 字数 3074 浏览 2 评论 0原文

我有一个 Joomla 组件,它调用辅助函数来创建验证码图像。禁用 sh404 时一切正常,但启用 sh404 时,安全图像的会话变量未正确设置,因此当您提交表单时,您会收到“无效验证码”消息。有趣的是,如果您再提交 5-6 次,它会验证正常并提交。我已经尝试了几乎所有我能想到的东西 - 当我回显会话变量并提交验证码时,会话似乎落后了一步 - 例如:

如果我第一次提交表单并回显会话变量并提交代码中,看起来会话变量没有及时设置 - 我得到会话变量的空白值。然后我再次提交表单,会话变量是上一个验证码图像的值。这是生成和验证验证码的代码。谢谢!

//Generate Captcha image link
function Captchalink($capid = ''){
    return 'index.php?option=com_mycomponent&view=home&task=newCaptcha&capid='.$capid;
}

function generateCode($characters) {
    /* list all possible characters, similar looking characters and vowels have been removed */
    $possible   = '23456789bcdfghjkmnpqrstvwxyz';
    $code       = '';
    $i          = 0;
    while ($i < $characters) {
        $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
        $i++;
    }
    return $code;
}

function CaptchaSecurityImages($capid = '') {

    $font       = dirname(__FILE__).DS."monofont.ttf";
    $width      = 90;
    $height     = 30;
    $characters = 6;
    $session    =& JFactory::getSession();

    //Clean buffers
    while (ob_get_level()) {
       ob_end_clean();
    }

    // start output buffering
    if (ob_get_length() === false) {
       ob_start();
    }
    $code = mycomponentHTML::generateCode($characters);

    /* font size will be 75% of the image height */
    $font_size          = $height * 0.75;
    $image              = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');

    /* set the colors */
    $background_color   = imagecolorallocate($image, 255, 255, 255);
    $text_color         = imagecolorallocate($image, 20, 40, 100);
    $noise_color        = imagecolorallocate($image, 100, 120, 180);

    /* generate random dots in background */
    for( $i=0; $i<($width*$height)/3; $i++ ) {
        imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }

    /* generate random lines in background */
    for( $i=0; $i<($width*$height)/150; $i++ ) {
        imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
    }

    /* create textbox and add text */
    $textbox = imagettfbbox($font_size, 0, $font, $code)  or die('Error in imagettfbbox function');
    $x = ($width - $textbox[4])/2;
    $y = ($height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code) or die('Error in imagettftext function');

    /* output captcha image to browser */
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);

    /* set session variable for newly created code */
    $session->set('security_code_'.$capid, md5($code));

    ob_end_flush();
    die();

}

function CaptchaValidate($capid = ''){
    $session    =& JFactory::getSession();
    if( $session->get('security_code_'.$capid) == md5(JRequest::getVar('security_code_'.$capid)) ) {
        $session->clear('security_code_'.$capid);
        return true;
    }else{
        return false;
    }
}

I have a Joomla component which calls a helper function to create a captcha image. Everything works fine when sh404 is disabled, but when sh404 is enabled the session variable for the security image isn't being set correctly so when you submit the form you get 'Invalid Captcha' message. The funny thing is if you submit another 5-6 times, it validates fine and submits. I've tried just about everything I can think of - when I echo the session variable and submitted captcha code it appears that the session is a step behind - for example:

If I submit the form the first time and echo the session variable and submitted code, it looks like the session variable is not being set in time - I get a blank value for the session variable. Then I submit the form again and the session variable is the value of the previous captcha image. Here's the code that generates and validates the captcha. Thanks!

//Generate Captcha image link
function Captchalink($capid = ''){
    return 'index.php?option=com_mycomponent&view=home&task=newCaptcha&capid='.$capid;
}

function generateCode($characters) {
    /* list all possible characters, similar looking characters and vowels have been removed */
    $possible   = '23456789bcdfghjkmnpqrstvwxyz';
    $code       = '';
    $i          = 0;
    while ($i < $characters) {
        $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
        $i++;
    }
    return $code;
}

function CaptchaSecurityImages($capid = '') {

    $font       = dirname(__FILE__).DS."monofont.ttf";
    $width      = 90;
    $height     = 30;
    $characters = 6;
    $session    =& JFactory::getSession();

    //Clean buffers
    while (ob_get_level()) {
       ob_end_clean();
    }

    // start output buffering
    if (ob_get_length() === false) {
       ob_start();
    }
    $code = mycomponentHTML::generateCode($characters);

    /* font size will be 75% of the image height */
    $font_size          = $height * 0.75;
    $image              = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');

    /* set the colors */
    $background_color   = imagecolorallocate($image, 255, 255, 255);
    $text_color         = imagecolorallocate($image, 20, 40, 100);
    $noise_color        = imagecolorallocate($image, 100, 120, 180);

    /* generate random dots in background */
    for( $i=0; $i<($width*$height)/3; $i++ ) {
        imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }

    /* generate random lines in background */
    for( $i=0; $i<($width*$height)/150; $i++ ) {
        imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
    }

    /* create textbox and add text */
    $textbox = imagettfbbox($font_size, 0, $font, $code)  or die('Error in imagettfbbox function');
    $x = ($width - $textbox[4])/2;
    $y = ($height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code) or die('Error in imagettftext function');

    /* output captcha image to browser */
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);

    /* set session variable for newly created code */
    $session->set('security_code_'.$capid, md5($code));

    ob_end_flush();
    die();

}

function CaptchaValidate($capid = ''){
    $session    =& JFactory::getSession();
    if( $session->get('security_code_'.$capid) == md5(JRequest::getVar('security_code_'.$capid)) ) {
        $session->clear('security_code_'.$capid);
        return true;
    }else{
        return false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文