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 } ?> Quote Link to comment Share on other sites More sharing options...
Jabop Posted December 24, 2008 Share Posted December 24, 2008 Works for me. Quote Link to comment 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 } ?> Quote Link to comment 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]; } ?> Quote Link to comment 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; ?> Quote Link to comment 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? Quote Link to comment 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" Quote Link to comment 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)]; } Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.