duanjian3338 2012-12-31 15:59
浏览 100
已采纳

PHP Captcha - 会话总是空的

Im trying to add a captcha to my registration form. The image is rendering just fine except the session variable is always empty, so it doesn't work at all..

I am using the following class : http://www.ejeliot.com/pages/php-captcha

According to the documentation i had to make a php file with the following content:

<?php
require('classes/PhpCaptcha.php');
$aFonts = array('fonts/VeraBd.ttf', 'fonts/VeraIt.ttf', 'fonts/Vera.ttf');
$oVisualCaptcha = new PhpCaptcha($aFonts, 200, 60);
$oVisualCaptcha->SetBackgroundImages('captchaimgbg.jpg');
$oVisualCaptcha->Create();
?>

And include this as an image.

Now ive looked into the Create(); function, and in this function another function named GenerateCode(); is called, which is responsible for generating the letters and putting it into a session variable.

The code is as follow:

      function GenerateCode() {
     // reset code
     $this->sCode = '';

     // loop through and generate the code letter by letter
     for ($i = 0; $i < $this->iNumChars; $i++) {
        if (count($this->aCharSet) > 0) {
           // select random character and add to code string
           $this->sCode .= $this->aCharSet[array_rand($this->aCharSet)];
        } else {
           // select random character and add to code string
           $this->sCode .= chr(rand(65, 90));
        }
     }

     // save code in session variable
     if ($this->bCaseInsensitive) {
        $_SESSION[CAPTCHA_SESSION_ID] = strtoupper($this->sCode);
     } else {
        $_SESSION[CAPTCHA_SESSION_ID] = $this->sCode;
     }
  }

The CAPTCHA_SESSION_ID is defined at the top of the class file. In my index i have session_start(); , i double checked this and that is not the problem.

No matter what i try, $_SESSION is always empty, even if i add $_SESSION['bla'] = 'bla' manually in the create(); function, it doesn't show up.

Does anyone have an idea what is causing this, or what i am doing wrong?

Thanks!

  • 写回答

1条回答 默认 最新

  • doo58088 2012-12-31 16:05
    关注
    <?php
    
    session_start();
    require('classes/PhpCaptcha.php');
    $aFonts = array('fonts/VeraBd.ttf', 'fonts/VeraIt.ttf', 'fonts/Vera.ttf');
    $oVisualCaptcha = new PhpCaptcha($aFonts, 200, 60);
    $oVisualCaptcha->SetBackgroundImages('captchaimgbg.jpg');
    $oVisualCaptcha->Create();
    
    ?>
    

    You need to start the session with session_start() at the TOP of the code on all the pages that access/set $_SESSION variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题