Jump to content

[SOLVED] Looking for a workaround with a loop


slpctrl

Recommended Posts

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 :)

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

 

 

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. :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.