Jump to content

Captcha code Error


peranha

Recommended Posts

I have this captcha, and it say undefined variable str on line 24, it worked before, but now doesnt.  Not sure why it quit working.

 

<?php
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<6;$i++){
    $pos = rand(0,36);
    $str .= $string{$pos}; //Line 24
}
?>

Link to comment
Share on other sites

You should also be using [] brackets for string indexing, {} brackets are becoming depricated.

 

<?php
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = NULL;
for($i=0;$i<6;$i++){
    $pos = rand(0,36);
    $str .= $string[$pos];
}
?>

Link to comment
Share on other sites

Thanks, that fixed that error, but now it will not set the $str in a session for me to check on submit.  Here is the code.

 

<?php
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = NULL;
for($i=0;$i<6;$i++){
    $pos = rand(0,36);
    $str .= $string[$pos];
}
$textbox = imagettfbbox($fontSize, 0, $font, $str) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $font , $str) or die('Error in imagettftext function');
for($i=0;$i<$lineCount;$i++){
    $x1 = rand(0,$width);$x2 = rand(0,$width);
    $y1 = rand(0,$width);$y2 = rand(0,$width);
    imageline($img_handle,$x1,$y1,$x2,$y2,$lineColor);
}
session_start();
header('Content-Type: image/jpeg');
imagejpeg($img_handle,NULL,100);
imagedestroy($img_handle);

$_SESSION['img_number'] = $str;
?>

Link to comment
Share on other sites

but now it will not set the $str in a session for me to check on submit

 

What tests have you done?

 

echo out the session, and it is empty, not set, moved the $_SESSION['img_number'] around, and still nothing.  Submitted the code, and it does not validate, keeps giving me "Undefined index: img_number" 

Link to comment
Share on other sites

Id replace this....

 

$string = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = NULL;
for($i=0;$i<6;$i++){
    $pos = rand(0,36);
    $str .= $string[$pos];
}

 

with....

 

$arr = array_merge(range('a','z'),range(0,9));
$str = '';
for ($i = 0; $i < 6; $i++) {
    $str .= $arr[rand(0,36)];
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.