简体   繁体   中英

PHP Captcha - Session always empty

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!

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM