PHP Captcha Problem

Discussion in 'PHP' started by maji_lg, Dec 12, 2007.

  1. #1
    Hi,

    I am new to php coding and I make a captcha by downloading code from net. But I have a problem during checking of captcha. I have used this code for making captcha image.

    <?php
    session_start();

    class CaptchaSecurityImages {

    var $font = 'monofont.ttf';

    function generateCode($characters) {
    /* list all possible characters, similar looking characters and vowels have been removed */
    $possible = '23456789bcdfghjkmnpqrstvwxyz';
    $code = '';
    $i = 0;
    while ($i < $characters) {
    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
    $i++;
    }
    return $code;
    }

    function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
    $code = $this->generateCode($characters);
    /* font size will be 75% of the image height */
    $font_size = $height * 0.75;
    $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
    /* set the colours */
    $background_color = imagecolorallocate($image, 255, 255, 255);
    $text_color = imagecolorallocate($image, 20, 40, 100);
    $noise_color = imagecolorallocate($image, 100, 120, 180);
    /* generate random dots in background */
    for( $i=0; $i<($width*$height)/3; $i++ ) {
    imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }
    /* generate random lines in background */
    for( $i=0; $i<($width*$height)/150; $i++ ) {
    imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
    }
    /* create textbox and add text */
    $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
    $x = ($width - $textbox[4])/2;
    $y = ($height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
    /* output captcha image to browser */
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    $_SESSION['security_code'] = $code;
    }

    }

    $width = isset($_GET['width']) ? $_GET['width'] : '120';
    $height = isset($_GET['height']) ? $_GET['height'] : '40';
    $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

    $captcha = new CaptchaSecurityImages($width,$height,$characters);

    ?>


    After that in form page I have sourcing this page as a image.

    In same page I have also done coding for captcha checking. Code is shown below


    <?php
    session_start();

    if(isset($_REQUEST['Submit'])){

    $name = stripslashes($_POST['Name']);
    $email = stripslashes($_POST['Email']);
    $phone = stripslashes($_POST['Phone']);
    $fax = stripslashes($_POST['Fax']);
    $msgbody = stripslashes($_POST['Remarks']);


    if (isset($_SESSION['security_code'])) {
    $key=$_SESSION['security_code'];
    }

    $number = stripslashes($_POST['number']);


    echo "key: $key";
    echo "number: $number";



    if($number==$key)
    {

    error_reporting(0);
    $recipient = 'test@test.com';
    $change = str_replace("\r\n", "<BR>", $msgbody);



    $subject ="Testing";

    $message ="<br><table width='95%' border='0' cellpadding='0' cellspacing='0' > <tr><td valign='top'><strong><font size='2' face='Verdana, Arial, Helvetica,ans-serif'>Dear <font color='#9900cc'>Web Administrator</font>,</font></strong><font size='2' face='Verdana, Arial, Helvetica, sans-serif'><br><br>One of the Visitors at <a href='http://www.agrasys.net' target='_blank'>www.agrasys.net</a> has contacted you with the following details -</font><br><br><table width='500' border='1' cellpadding='2' cellspacing='0' bordercolor='#FFFFFF'>";
    $message.="<tr> <td width='20%' bgcolor='#ececfc'><font color='000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'><b>Name</b></font></td><td width='80%' ><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'>$name</font></td></tr><tr> <td width='20%' bgcolor='#ececfc'><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'><b>Phone</b></font></td><td width='80%' ><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'>$phone</font></td></tr><tr> <td width='20%' bgcolor='#ececfc'><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'><b>Fax</b></font></td><td width='80%' ><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'>$fax</font></td></tr><tr><td width='20%' bgcolor='#ececfc'><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'><b>E-mail</b></font></td><td width='80%' ><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'><a href='mailto:$email'>$email</a></font></td></tr><tr><td width='20%' valign='top' bgcolor='#ececfc'><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'><b>Remarks</b></font></td><td width='80%' ><font color='#000066' size='2' face='Verdana, Arial, Helvetica, sans-serif'><p align='justify'>$change</p></font></td></tr>";
    $message.="</table><br><br><strong><font size='2' face='Verdana, Arial, Helvetica, sans-serif'>Best Regards,</font></strong><font size='2' face='Verdana, Arial, Helvetica, sans-serif'><br><a href='http://www.agrasys.net'><font color='#9900cc'><b>www.agrasys.net</b></font></a></font><br> </td></tr></table><p></p>";


    $headers = "MIME-Version: 1.0\r\n";
    $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers.= "From: $email\r\n";

    mail($recipient, $subject, $message, $headers);

    }
    }
    ?>

    I print the key and number which is marked as bold above, it doesn't match though I have filled the correct captcha image value in text field in form.

    Sometimes it match with captcha value sometime not.

    Plz help me to solve this problem.

    Thanks in advance.
     
    maji_lg, Dec 12, 2007 IP
  2. daman371

    daman371 Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    One thing that may help is unsetting the session variable and destroying the session after the end of script.
     
    daman371, Dec 18, 2007 IP