Jump to content

PHP Captchas


limitphp

Recommended Posts

Here's one I wrote:

<?php
  session_start();
  $imgWidth=130;
  $imgHeight=34;
  $chars='abcdefghijkmnqrstuvwxyz0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ';
  $strText='';
  for ($i=1;$i<7;$i++) {$strText.=substr($chars,mt_rand(0,strlen($chars)-1),1);}
  $_SESSION['vercode']=md5($strText);
  header("Content-type: image/png");
  $img=imagecreatetruecolor($imgWidth,$imgHeight); //enable if plain colored background is to be used
  //$img=imagecreatefrompng('gfx/codebg.png'); //enable if patterned background image is to be used
  $colBG=imagecolorallocate($img,0x11,0x11,0x11);
  for ($i=0;$i<strlen($strText);$i++) {
    $colFG=imagecolorallocate($img,mt_rand(50,255),mt_rand(50,255),mt_rand(50,255));
    imagettftext($img,20,mt_rand(-40,40),10+($i*20),27,$colFG,'fonts/times.ttf',substr($strText,$i,1));
  }
  imagepng($img);
  imagedestroy($img);
?>

 

Just make sure your TFF font is in the same place as specified by the imagettftext line.

 

Just MD5 the input from your user and check it with $_SESSION['vercode']

 

Call with:

<img src="makeimg.php?<?=mt_rand(1,100000)?>" />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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