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 6084905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:34:42+00:00 2026-05-23T11:34:42+00:00

I am trying to add a simple captcha to an hml form. Code is

  • 0

I am trying to add a simple captcha to an hml form. Code is originally from here

here is the PHP script that generates the image and stores the captcha variable:

<?php

session_start();

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 

header("Cache-Control: no-store, no-cache, must-revalidate"); 

header("Cache-Control: post-check=0, pre-check=0", false);

header("Pragma: no-cache"); 



function _generateRandom($length=6)

{

    $_rand_src = array(

        array(48,57) //digits

        , array(97,122) //lowercase chars

//      , array(65,90) //uppercase chars

    );

    srand ((double) microtime() * 1000000);

    $random_string = "";

    for($i=0;$i<$length;$i++){

        $i1=rand(0,sizeof($_rand_src)-1);

        $random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));

    }

    return $random_string;

}



$im = @imagecreatefromjpeg("captcha.jpg"); 

$rand = _generateRandom(3);

$_SESSION['captcha'] = $rand;

ImageString($im, 5, 2, 2, $rand[0]." ".$rand[1]." ".$rand[2]." ", ImageColorAllocate ($im, 0, 0, 0));

$rand = _generateRandom(3);

ImageString($im, 5, 2, 2, " ".$rand[0]." ".$rand[1]." ".$rand[2], ImageColorAllocate ($im, 255, 0, 0));

Header ('Content-type: image/jpeg');

imagejpeg($im,NULL,100);

ImageDestroy($im);

?>

here is the script that validates the entered captcha:

<?php

if($_SESSION["captcha"]==$_POST["captcha"])

{

    //CAPTHCA is valid; proceed the message: save to database, send by e-mail ...

    echo 'CAPTHCA is valid; proceed the message';

}

else

{

    echo 'CAPTHCA is not valid; ignore submission';

}

?>

Problem is that the session seems to be storing only 3 chars, so the values never match – since the generated image has 6 chars.

The code above is a bit strange (I must admit I am not used the image library API). But I dont know why we call _generateRandom() with arg value of 3, and also, why imagestring is being called twice ???

  • 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-23T11:34:43+00:00Added an answer on May 23, 2026 at 11:34 am

    Display the following text alongside the CAPTCHA input:
    “Enter the 3 black symbols (ignore the red ones)”

    😀

    Since only the black characters are stored in the Session.

    UPDATE: I checked the link you provided and it has the text:
    “(antispam code, 3 black symbols)” above the CAPTCHA input box.

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

Sidebar

Related Questions

I have a form that I am trying to add a simple select element
I am trying to write simple Visual Studio Add-In for code generation. In my
I am trying to write a really simple Outlook VSTO add in that checks
I am trying to create a simple ajax grid that allows me to add
I have a plot (sample code pasted below) that I am trying to add
I am trying to add some simple validation to my asp.net mvc form and
I have this simple code: <xsl:for-each select=GroupsServed> <xsl:value-of select=./>,<br/> </xsl:for-each></font> I'm trying to add
I'm trying to add a simple datepicker to my form and it is working
I am trying to add a simple figure into my latex code .I have
I am trying to add just a simple Twitter icon that will make the

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.