Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php captcha code not working

10:23 pm on May 31, 2012 (gmt 0)

Junior Member from IT 

5+ Year Member

joined:Mar 17, 2009
posts: 60
votes: 0

I'm using an old random() function for creating a validation code for an AJAX commenting system I found on the web (source code at [tutorialzine.com...] ).

The idea behind is pretty simple:

function Random()
$i = 0;
$pass = '' ;
while ($i <= 4)
$num = rand() % 32;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
return $pass;
$random_code = Random();

and then in the form, just before the SUBMIT button:

<label for="security_code">Enter this captcha code: <b><? echo $random_code; ?></b></label> 
<input type="text" name="security_code" id="security_code" />

<input name="randomness" type="hidden" id="randomness" value="<?php $random_code; ?>">

My AJAX commenting system use something like this for checking if a field is blank (ie. if there are any errors):

$errors = array();
$data= array();

if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
$errors['name'] = 'Please enter a name.';


so I wrote this:

if(!($data['security_code'] = filter_input(INPUT_POST,'security_code',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
$errors['security_code'] = 'You did not enter the validation code.';
elseif(!($data['security_code'] = $randomness))
$errors['security_code'] = 'You entered the validation code incorrectly. Please note that it is case sensitive.';

However when I click on the SUBMIT button ( test it by yourself at [flapane.com...] ) I always get the "You entered the validation code incorrectly. Please note that it is case sensitive" message.
What am I missing? I wonder if the original captcha code gets lost at some point.

Thanks in advance