peranha Posted December 24, 2008 Share Posted December 24, 2008 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 https://forums.phpfreaks.com/topic/138258-captcha-code-error/ Share on other sites More sharing options...
Jabop Posted December 24, 2008 Share Posted December 24, 2008 Works for me. Link to comment https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722849 Share on other sites More sharing options...
ToonMariner Posted December 24, 2008 Share Posted December 24, 2008 try... <?php $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = NULL; for($i=0;$i<6;$i++){ $pos = rand(0,36); $str .= $string{$pos}; //Line 24 } ?> Link to comment https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722864 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 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 https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722874 Share on other sites More sharing options...
peranha Posted December 24, 2008 Author Share Posted December 24, 2008 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 https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722904 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 but now it will not set the $str in a session for me to check on submit What tests have you done? Link to comment https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722912 Share on other sites More sharing options...
peranha Posted December 24, 2008 Author Share Posted December 24, 2008 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 https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722919 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 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 https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722923 Share on other sites More sharing options...
peranha Posted December 24, 2008 Author Share Posted December 24, 2008 Yeah, but that still doesnt help with the session not getting set. I am not sure why it isnt. Link to comment https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722931 Share on other sites More sharing options...
trq Posted December 24, 2008 Share Posted December 24, 2008 Have you got session_start() on all pages using sessions? Link to comment https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722943 Share on other sites More sharing options...
peranha Posted December 24, 2008 Author Share Posted December 24, 2008 Have you got session_start() on all pages using sessions? Yes I do, it is included in my header file. When I insert it into this file, it says A session had already been started - ignoring session_start() Link to comment https://forums.phpfreaks.com/topic/138258-captcha-code-error/#findComment-722968 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.