Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6058803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:38:24+00:00 2026-05-23T08:38:24+00:00

I’m trying to implement a captcha into a form. There are three files: the

  • 0

I’m trying to implement a captcha into a form.

There are three files:

the html form.

The php captcha image generator.

the php captcha checker.

Everythiong is working fine, but I can’t seem to retrieve the Session variable, that the captcha script creates, on my captcha checker script.

In the form:

<img src="captcha/captcha.php"></img>
<input type="text" id="CAPTCHA"><p id="caperror"></p>
<p><input name="B1" type="submit" value="Submit" />
</form> 

<script>
$('#contactform').submit(function () {
    var cap = $('#CAPTCHA').val();
    cap = 'CAPTCHA=' + cap;
    $.ajax({
        type: 'POST',
        url: 'captcha/capcheck.php',
        data: cap,
        success: success,
        dataType: "text",
        error: postfail
    });
});

function success(result) {
    if (result == 'true') {
        alert('Correct');
        return true;
    } else {
        alert(result);
        return false;
    }
}

function postfail() {
    alert('post failed');
    return false;
}
</script>

The session Variable [‘CAPTCHA’] is generated in the captcha.php script.

I try to retrieve it, and check it im the capcheck.php script with this:

<?php 
session_start();  

if($SESSION['CAPTCHA']) {
echo 'session='.$SESSION['CAPTCHA'];
}
if((isset($POST['CAPTCHA'])) && (isset($SESSION['CAPTCHA']))){
if($_SESSION['CAPTCHA'] != $_POST['CAPTCHA']) {

    echo 'false';


    } else {
    session_destroy();
        echo 'true';
}

}
else{
echo $_POST['CAPTCHA'].'    '. $SESSION['CAPTCHA'];
session_destroy();
}
?>

It is always empty, and I don’t know why! I have it implemented without jquery, on a different site, and it works perfectly.

Captcha.php:

    <?php

        session_start();

        /**
         * Send PNG headers to the browser
        **/
        header('content-type: image/png');

        $numbers    = array();
        $numbers[]  = rand(1, 5);
        $numbers[]  = rand(1, 5);

        if(rand(0, 1) == 0) {
            $_op                        = '+'; 
            $code                       = strval($numbers[0]) .' + ' .strval($numbers[1]) .' =';
            $_SESSION['CAPTCHA'] = $numbers[0] + $numbers[1];
        } else {
            $_op                        = '+';  
            $code                       = strval($numbers[0]) .' + ' .strval($numbers[1]) .' =';
            $_SESSION['CAPTCHA'] = $numbers[0] + $numbers[1];
        }
        session_write_close();

        /* Our TTF font file, you may use others */
        $font = dirname(__FILE__) .'/fonts/arial.ttf';

        /* set the width */
        $width  = 16;
        //$width  = (strlen($code) * $width) + 2;   
        $height = 24;

        $code_length = 7;

        $image_height = $height + 2;
        $image_width  = $width * $code_length + 20;

        $im       = imagecreatetruecolor($image_width, $image_height);
        $white  = imagecolorallocate($im, 255, 255, 255);
        imagefill($im, 0, 0, $white);

        /* Some themes */
        $theme   = array();
        $theme[]    = array('CHAR_COLOR' => 
                                array('R' => array(56, 21),
                                        'G' => array(85, 25),
                                        'B' => array(14, 195) 
                                        ),
                               'BG_COLOR' =>
                                array('R' => array(12, 76),
                                        'G' => array(54, 67),
                                        'B' => array(26, 23) 
                                        )
                             );
        $theme[]    = array('CHAR_COLOR' => 
                                array('R' => array(59, 72),
                                        'G' => array(35, 55),
                                        'B' => array(65, 63) 
                                        ),
                               'BG_COLOR' =>
                                array('R' => array(30, 55),
                                        'G' => array(70, 30),
                                        'B' => array(70, 30) 
                                        )
                             );
        $theme[]    = array('CHAR_COLOR' => 
                                array('R' => array(76, 25),
                                        'G' => array(38, 50),
                                        'B' => array(58, 78) 
                                        ),
                                'BG_COLOR' =>
                                array('R' => array(94, 30),
                                        'G' => array(97, 30),
                                        'B' => array(30, 55) 
                                        )
                             );

        $pos_x  = 5;
        $pos_y  = 20;
        $random = rand(0, (count($theme) - 1) );/* Get a random theme */

        /**
         *  Place each character into the image 
        **/
        $angle  = 0;
        $size      = 16;
        for($i = 0, $count = strlen($code); $i < $count; $i++) {

            $color  = imagecolorallocate($im, 
                                                  rand($theme[$random]['CHAR_COLOR']['R'][0], $theme[$random]['CHAR_COLOR']['R'][1]), 
                                                  rand($theme[$random]['CHAR_COLOR']['G'][0], $theme[$random]['CHAR_COLOR']['G'][1]), 
                                                  rand($theme[$random]['CHAR_COLOR']['B'][0], $theme[$random]['CHAR_COLOR']['B'][1])
                                                  );        

            imagettftext($im, $size, $angle, $pos_x, $pos_y, $color, $font, $code{$i});
            $pos_x  += $width + 1;

        }

        /* Finally show image */
        imagepng($im);  
        imagedestroy($im);  
    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:38:25+00:00Added an answer on May 23, 2026 at 8:38 am

    Some of your $POST and $SESSION variables are missing underscores. As mentioned in your comments, this seems the be the issue.

    I’d suggest using a text-editor with proper syntax highlighting support for PHP in this case, so errors like these are easily identified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.