slpctrl Posted September 11, 2008 Share Posted September 11, 2008 Alright, I was having a bit of trouble on here a while ago. Here is my code for my CAPTCHA: <?php session_start(); $hi = yes.jpg; $str1 = str_shuffle(md5(microtime() * mktime())); sleep(.5); $str2 = str_shuffle(md5(microtime() * mktime())); $str = substr($str1,0,5); $xtra1 = substr($str2,0,5); $xtra2 = substr($str2,5,10); $xtra3 = substr($str2,10,15); $xtra4 = substr($str2,15,20); $xtra5 = substr($str2,20,25); $captcha = imagecreatefrompng("./captcha.png"); $color = imagecolorallocatealpha($captcha,255,0,0,40); $linecolor = imagecolorallocatealpha($captcha,255,0,0,rand(0,100)); $textcolor = imagecolorallocatealpha($captcha,230,0,0,100); for($i = 0; $i < 20; $i++) { imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$linecolor); } imagestring($captcha,3,rand(0,100),rand(0,50),$str,$color); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra1,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra2,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra3,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra4,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra5,$textcolor); $_SESSION['str'] = md5($str); header("Content-type: image/png"); imagepng($captcha); ?> What I'm trying to do is make it a bit less redundant here: <? imagestring($captcha,3,rand(0,100),rand(0,50),$str,$color); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra1,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra2,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra3,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra4,$textcolor); imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra5,$textcolor); ?> But nothing really seems to come to mind, loop wise to make my code neater and cut down on the lines, even with an array I wouldn't really know how to do it. Can anyone else help me to do all this with a loop like I did here: <? for($i = 0; $i < 20; $i++) { imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$linecolor); } ?> Thanks Link to comment https://forums.phpfreaks.com/topic/123813-solved-looking-for-a-workaround-with-a-loop/ Share on other sites More sharing options...
BlueSkyIS Posted September 11, 2008 Share Posted September 11, 2008 something like.. <? imagestring($captcha,3,rand(0,100),rand(0,50),$str,$color); for ($i=1;$i<=5;$i++) { imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),${'xtra'.$i},$textcolor); } ?> Link to comment https://forums.phpfreaks.com/topic/123813-solved-looking-for-a-workaround-with-a-loop/#findComment-639283 Share on other sites More sharing options...
kenrbnsn Posted September 11, 2008 Share Posted September 11, 2008 Try: <?php session_start(); $xtra = array(); $hi = 'yes.jpg'; $str1 = str_shuffle(md5(microtime() * mktime())); sleep(.5); $str2 = str_shuffle(md5(microtime() * mktime())); $str = substr($str1,0,5); for ($i=0;$i<5;$i++) { $st = $i * 5; $ed = ($i + 1) * 5; $xtra[$i] = substr($str2,$st,$ed); } $captcha = imagecreatefrompng("./captcha.png"); $color = imagecolorallocatealpha($captcha,255,0,0,40); $linecolor = imagecolorallocatealpha($captcha,255,0,0,rand(0,100)); $textcolor = imagecolorallocatealpha($captcha,230,0,0,100); for($i = 0; $i < 20; $i++) { imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$linecolor); } imagestring($captcha,3,rand(0,100),rand(0,50),$str,$color); for ($i=0;$i<5;$i++) { imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra[$i],$textcolor); } $_SESSION['str'] = md5($str); header("Content-type: image/png"); imagepng($captcha); ?> Note: untested. Ken Link to comment https://forums.phpfreaks.com/topic/123813-solved-looking-for-a-workaround-with-a-loop/#findComment-639285 Share on other sites More sharing options...
slpctrl Posted September 11, 2008 Author Share Posted September 11, 2008 Try: <?php session_start(); $xtra = array(); $hi = 'yes.jpg'; $str1 = str_shuffle(md5(microtime() * mktime())); sleep(.5); $str2 = str_shuffle(md5(microtime() * mktime())); $str = substr($str1,0,5); for ($i=0;$i<5;$i++) { $st = $i * 5; $ed = ($i + 1) * 5; $xtra[$i] = substr($str2,$st,$ed); } $captcha = imagecreatefrompng("./captcha.png"); $color = imagecolorallocatealpha($captcha,255,0,0,40); $linecolor = imagecolorallocatealpha($captcha,255,0,0,rand(0,100)); $textcolor = imagecolorallocatealpha($captcha,230,0,0,100); for($i = 0; $i < 20; $i++) { imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$linecolor); } imagestring($captcha,3,rand(0,100),rand(0,50),$str,$color); for ($i=0;$i<5;$i++) { imagestring($captcha,rand(1,5),rand(0,200),rand(0,70),$xtra[$i],$textcolor); } $_SESSION['str'] = md5($str); header("Content-type: image/png"); imagepng($captcha); ?> Note: untested. Ken Ahhh, that works . Should have thought of that, thanks. Link to comment https://forums.phpfreaks.com/topic/123813-solved-looking-for-a-workaround-with-a-loop/#findComment-639289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.