Jump to content

CAPTCHA code


john010117
 Share

Recommended Posts

Since I have very little experience in working with GD library, I need advice on how to improve this simple CAPTCHA code (don't worry, I have other security precautions in place, since CAPTHA aren't that secure). I've only included the page that actually displays the image.

 

<?php
session_start();

$RandomStr = md5(microtime());// md5 to generate the random string

$ResultStr = substr($RandomStr,0,5);//trim 5 digit

// Select a random background image
$dir=opendir("./images/captcha/");

$i=0;
while($imgfile=readdir($dir)) {
   if ($imgfile != "." && $imgfile!="..") {
      $imgarray[$i]=$imgfile;
      $i++;
   }
}

closedir($dir);

$rand=rand(0,count($imgarray)-1);

if($rand >= 0) {
   $RandomImage = $imgarray[$rand];
}

$NewImage =imagecreatefrompng("./images/captcha/$RandomImage");//image create by existing image and as background

$randcolR = rand(100,230);
$randcolG = rand(100,230);
$randcolB = rand(100,230);

$LineColor = imagecolorallocate($NewImage,233,239,239);//line color 
$TextColor = imagecolorallocate($NewImage, ($randcolR - 20), ($randcolG - 20), ($randcolB - 20));

imageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image 
imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image 

imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally 

$_SESSION['key'] = $ResultStr;// carry the data through session

header("Content-type: image/png");// out out the image 

imagepng($NewImage);//Output image to browser
imageDestroy($NewImage);
?>

 

Before you suggest any suggestions, please not that I only have GD library available on my server (that means no FreeType). Ok, now, any suggestions please? :D

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

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