codeboy89 Posted November 21, 2009 Share Posted November 21, 2009 Below is my CAPTCHA code, can anyone tell me why sometimes it displays the 6 numbers like it should, but other times it displays only 4 numbers? I cannot figure it out. <?php session_start(); $number = rand(1,999999); //generate a random integer $_SESSION['number'] = $number; //store in session variable $img_number = imagecreate(75,25); $backcolor = imagecolorallocate($img_number,0xcc,0xcc,0xcc); $textcolor = imagecolorallocate($img_number,255,255,255); imagefill($img_number,0,0,$backcolor); imagestring($img_number,3,5,5,$number,$textcolor); header("Content-type: image/jpeg"); imagejpeg($img_number); ?> Link to comment https://forums.phpfreaks.com/topic/182414-can-you-figure-this-captcha-out/ Share on other sites More sharing options...
abazoskib Posted November 21, 2009 Share Posted November 21, 2009 $number = rand(1,999999); //generate a random integer Your function here will generate any number in between 1 and 999999. If you want a six digit number, use $number = rand(100000,1000000); //generate a random integer Link to comment https://forums.phpfreaks.com/topic/182414-can-you-figure-this-captcha-out/#findComment-962581 Share on other sites More sharing options...
codeboy89 Posted November 21, 2009 Author Share Posted November 21, 2009 thanks!! Link to comment https://forums.phpfreaks.com/topic/182414-can-you-figure-this-captcha-out/#findComment-962584 Share on other sites More sharing options...
salathe Posted November 21, 2009 Share Posted November 21, 2009 There is a slight chance that the number generated by abazoskib will be 1000000 (i.e., 7 digits), to only ever get 6-digit numbers you'll need to call rand(100000, 999999). Link to comment https://forums.phpfreaks.com/topic/182414-can-you-figure-this-captcha-out/#findComment-962632 Share on other sites More sharing options...
abazoskib Posted November 21, 2009 Share Posted November 21, 2009 My mistake, I thought it generated numbers BETWEEN those two. salathe is correct. Link to comment https://forums.phpfreaks.com/topic/182414-can-you-figure-this-captcha-out/#findComment-962638 Share on other sites More sharing options...
waynew Posted November 21, 2009 Share Posted November 21, 2009 I made your captcha a little bit tougher: <?php $charset = 'A,B,C,D,E,F,G,H,K,L,M,N,P,R,S,T,U,V,W,Y,Z,1,2,3,4,5,6,7,8,9,!'; $char_array = explode(",",$charset); $size_of_array = sizeof($char_array) - 1; $num_chars_in_capthca = 6; $captcha_string = ""; $i = 0; while($i < $num_chars_in_capthca){ $index = rand(0,$size_of_array); $captcha_string .= $char_array[$index]; $i++; } $_SESSION['captcha_code'] = $captcha_string; $img_number = imagecreate(75,25); $backcolor = imagecolorallocate($img_number,0xcc,0xcc,0xcc); $textcolor = imagecolorallocate($img_number,255,255,255); imagefill($img_number,0,0,$backcolor); imagestring($img_number,3,5,5,$captcha_string,$textcolor); header("Content-type: image/jpeg"); imagejpeg($img_number); ?> Link to comment https://forums.phpfreaks.com/topic/182414-can-you-figure-this-captcha-out/#findComment-962642 Share on other sites More sharing options...
waynew Posted November 21, 2009 Share Posted November 21, 2009 I added lines this time: <?php $charset = 'A,B,C,D,E,F,G,H,K,L,M,N,P,R,S,T,U,V,W,Y,Z,1,2,3,4,5,6,7,8,9,!'; $char_array = explode(",",$charset); $size_of_array = sizeof($char_array) - 1; $num_chars_in_capthca = 6; $captcha_string = ""; $i = 0; while($i < $num_chars_in_capthca){ $index = rand(0,$size_of_array); $captcha_string .= $char_array[$index]; $i++; } $_SESSION['captcha_code'] = $captcha_string; $img_number = imagecreate(125,50); $backcolor = imagecolorallocate($img_number,000,000,000); $textcolor = imagecolorallocate($img_number,255,255,255); $linecolor = imagecolorallocate($img_number,232,128,240); imagefill($img_number,0,0,$backcolor); imagestring($img_number,10,35,17,$captcha_string,$textcolor); $num_lines = 3; $i = 0; while($i < $num_lines){ imageline ($img_number,rand(1,62),rand(1,25),rand(63,125),rand(26,50),$linecolor); $i++; } header("Content-type: image/jpeg"); imagejpeg($img_number); ?> Link to comment https://forums.phpfreaks.com/topic/182414-can-you-figure-this-captcha-out/#findComment-962657 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.