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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line