yasirmturk / simple-php-captcha

A simple PHP CAPTCHA script.
MIT License
371 stars 212 forks source link

PHP Notice: Undefined index: _CAPTCHA #14

Closed wilfi closed 9 years ago

wilfi commented 9 years ago

Im trying to load the image on ajax. The image is not loading and when I checked the log its showing as PHP Notice: Undefined index: _CAPTCHA Please help me out.

greatwitenorth commented 9 years ago

Same problem here. Was _CAPTCHA supposed to be defined somewhere?

claviska commented 9 years ago

Your error reporting is setup to show notices. If you change it to ignore notices you'll be fine. Otherwise, feel free to submit a PR :)

claviska commented 9 years ago

To do that, you have to generate the image and send back the source. A very basic example:

<?php
// captcha.php
session_start();
include("simple-php-captcha.php");
$_SESSION['captcha'] = simple_php_captcha();
echo $_SESSION['captcha']['image_src'];
?>

Now apply the resulting value to an image. jQuery example for clarity:

$.get('captcha.php', null, function(response) {
    $('#your-image-id').attr('src', response);
});

To verify the code, simply check against $_SESSION['captcha']['code'] when you submit the form. (Make sure to verify BEFORE calling simple_php_captcha() again, otherwise the code will get regenerated!)