Help Customizing Captchas

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
I need someone with a good expertise in php to solve this!
I got a nice captcha script here - http://rapidshare.com/files/130368778/ajax_captcha.zip.html
But I dint linke the image it generated so I searched and got another one-(quite beautiful)
PHP:
<?php
session_start();
define('CAPYAINC','/home/user/public_html/domain/anti spam/'); // the directory where the fonts and bg image are stored
define('CAPYAURI','http://domain/anti spam/');  // the web uri where the captcha image will be located
define('CAPYADIR','/home/user/public_html/domain/anti spam/'); //

function askapache_captcha($type=1,$numletters=4,$fontsize=22){
    $capya_string=capya_string($numletters);                // the letters and numbers displayed on captcha
    $capya_bgfile=CAPYAINC.'n.png';                            // the background image for the captcha
    $capya_filename='askapache-'.rand(1111,999999).'.jpg';        // the filename of finished captcha
    $capya_file=CAPYADIR.$capya_filename;                    // the full path to finished captcha
    $capya_uri=CAPYAURI.$capya_filename;                    // the public web address to finished captcha
    $rgb[0]=array(204,0,0);
    $rgb[1]=array(34,136,0);
    $rgb[2]=array(51,102,204);
    $rgb[3]=array(141,214,210);
    $rgb[4]=array(214,141,205);
    $rgb[5]=array(100,138,204);

    // create image from background image
    $image=imagecreatefrompng($capya_bgfile);
    
    // store the md5 of the captcha string
    $_SESSION['askapache_captcha'] = md5($capya_string);
    
    // add chars to captcha image
    $g=$fontsize;
    for($i=0; $i<$numletters; $i++){
        $L[]=substr($capya_string,$i,1);    // each char from string into individual variable
        $A[]=rand(-20, 20);            // random angle for each char
        $F[]=CAPYAINC.rand(1, 10).".ttf";  // random font for each char
        $C[]=rand(0, 5);            // random color for each char
        $T[]=imagecolorallocate($image,$rgb[$C[$i]][0],$rgb[$C[$i]][1],$rgb[$C[$i]][2]);    // allocate colors for chars
        imagettftext($image, $fontsize, $A[$i], $g, $fontsize+15, $T[$i], $F[$i], $L[$i]);    // write chars to image
        $g+=$fontsize+10;
    }

    // save jpeg image to public web folder
    imagejpeg($image, $capya_file);

    if($type===1){
    // output the image url
        echo '<p><img src="'.$capya_uri.'" alt="" width="150" height="50" /><label for="capya" class="S"><input id="capya" name="capya" type="text" value="" size="5" class="S" style="width:150px" maxlength="5" /></label></p>';
    } else echo '<img src="'.$capya_uri.'" alt="" width="150" height="50" />';
        
    
    // destroy image
    imagedestroy($image);

    // delete all captcha images at 12 and 3 oclock if more than 100 are found
    $dt=date('g');
    if(($dt==12)||($dt=='12'))capya_cleanup();
    else if(($dt==3)||($dt=='3'))capya_cleanup();
}

function capya_cleanup(){
$files=glob(CAPYADIR."apache*.jpg");
    if(sizeof($files)>100){
        foreach ($files as $filename) {
            unlink($filename);
            //echo "$filename size " . filesize($filename) . "\n";
        }
    }
}

function capya_string($len){
    $str='';
    for($i=1; $i<=$len; $i++) {
        $ord=rand(48, 90);
        if((($ord >= 48) && ($ord <= 57)) || (($ord >= 65) && ($ord<= 90))) $str.=chr($ord);
        else $str.=capya_string(1);
    }
    return $str;
}
?>
but I can't check if the user entered a correct image or not
So what I need is a cross between the two scripts so that I can generate the beautiful image and check it using Ajax
Any help would be greatly appreciated
Edit:
Please help the problem is very easy to solve for someone who knows just the right syntax. But I am totally new to php!
Edit:
I don't need your help now!
I figured it out(took me a couple of hours though)
Thank you for not helping me and exhibiting the feedbacks on this forum!
 
Last edited:

websoul

New Member
Messages
39
Reaction score
0
Points
0
y dont u tell what u did for solving this..so we can use it later
 

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
Just imagine my situation!
I posted this for getting help but no one helped me and it took me a great effort to do something which can be easily done by anyone who knows php!
Then why should I post my answer here?
Edit:
No wonder you had to make free users log in to this forum at least once a month to kkep it going
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
There's this line of code:
PHP:
$_SESSION['askapache_captcha'] = md5($capya_string);
Which stores the md5 of the captcha-string to $_SESSION['askapache_captcha'] (btw, weird name, and why md5 it? it's stored server-side, the user is unable to view it, unless he hacks the server, which is worse than a bot making an account)
So what you have to do is compare what the md5 of what the user entered with this variable (after you started the session, of course)

Since he wants to use AJAX, you probably will be using a $_GET variable to send whatever the user entered to the server. An example would be:
PHP:
session_start();
if($_SESSION['askapache_captcha'] == md5(rawurldecode($_GET['captcha']))) echo "true";
else echo "false";

Then all you have to do is make the JS to request that file (+ add the user's imput) and see what you get: true -> ok; false -> not ok.

- Marshian

Edit:
@ teensweb: at least you can say now you can do this crap too! =D
 
Last edited:

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
This is my code and it works fine-
HTML:
<html>
    <head>
        <script language="JavaScript" type="text/javascript" src="ajax_captcha.js"></script>
    </head>
    <body>
    <form id="frmCaptcha" name="frmCaptcha">
        <table> 
            <tr>
                <td align="left">
                    <label for="captcha">Captcha</label>
                </td>
                <td>
                    <input id="txtCaptcha" type="text" name="txtCaptcha" value="" maxlength="10" size="32" />
                </td>
                <td> 
                    <img id="imgCaptcha" src="create_image.php" />
                    
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input id="btnCaptcha" type="button" value="Captcha Test" name="btnCaptcha" 
                        onclick="getParam(document.frmCaptcha)" />
                </td>
            </tr>
        </table> 

        <div id="result">&nbsp;</div>
    </form>
    </body>
</html>
and I am using a contact form with the below code
PHP:
Contact Us</font></h1>
<p><em>Fields marked with <strong>*</strong> are required.</em></p>
<form name="form_contact" id="form_contact" method="post" action="contact.php">

<?php showSet("name"); ?>
<?php showSet("contact"); ?>
<?php showSet("profession"); ?>
<?php showSet("location"); ?>
<?php showSet("info"); ?>
<?php showSet("preferences"); ?>
<form id="frmCaptcha" name="frmCaptcha">

<div class="buttons"><button type="submit" class="positive" id="sendme" title="Send your message" onclick="getParam(document.frmCaptcha)" />
<img src="email_go.gif" alt="Send!"> 
Send!</button>
</div>
</form>
How do I integrate my captcha here?
my code to check the captcha is
PHP:
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) && 
    (!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {
  echo "<h1>Test successful!</h1>";
} else {
  echo "<h1>Test failed! Try again!</h1>";
}
?>
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
You say your code works fine, is your problem solved? Or do you still have a problem "How do I integrate my captcha here?" ? (And if so, what is it?)
 

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
I mean the first one is my captcha code and second is my contact form's code
I want to know how to place the cptcha in it
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
Well, first of all, you should put all elements in the same <form>. Second, that's a very poor solution in your main captcha script, I recommend changing it to this:
PHP:
<?php
session_start();
define('CAPYAINC','/home/user/public_html/domain/anti spam/'); // the directory where the fonts and bg image are stored
define('CAPYAURI','http://domain/anti spam/');  // the web uri where the captcha image will be located
define('CAPYADIR','/home/user/public_html/domain/anti spam/'); //

function askapache_captcha($type=1,$numletters=4,$fontsize=22){
    $capya_string=capya_string($numletters);                // the letters and numbers displayed on captcha
    $capya_bgfile=CAPYAINC.'n.png';                            // the background image for the captcha
    $capya_filename='askapache-'.rand(1111,999999).'.jpg';        // the filename of finished captcha
    $capya_file=CAPYADIR.$capya_filename;                    // the full path to finished captcha
    $capya_uri=CAPYAURI.$capya_filename;                    // the public web address to finished captcha
    $rgb[0]=array(204,0,0);
    $rgb[1]=array(34,136,0);
    $rgb[2]=array(51,102,204);
    $rgb[3]=array(141,214,210);
    $rgb[4]=array(214,141,205);
    $rgb[5]=array(100,138,204);

    // create image from background image
    $image=imagecreatefrompng($capya_bgfile);
    
    // store the md5 of the captcha string
    $_SESSION['askapache_captcha'] = md5($capya_string);
    
    // add chars to captcha image
    $g=$fontsize;
    for($i=0; $i<$numletters; $i++){
        $L[]=substr($capya_string,$i,1);    // each char from string into individual variable
        $A[]=rand(-20, 20);            // random angle for each char
        $F[]=CAPYAINC.rand(1, 10).".ttf";  // random font for each char
        $C[]=rand(0, 5);            // random color for each char
        $T[]=imagecolorallocate($image,$rgb[$C[$i]][0],$rgb[$C[$i]][1],$rgb[$C[$i]][2]);    // allocate colors for chars
        imagettftext($image, $fontsize, $A[$i], $g, $fontsize+15, $T[$i], $F[$i], $L[$i]);    // write chars to image
        $g+=$fontsize+10;
    }

    // Output the image directly to the user's browser.
	return imagejpeg($image);
    
    // destroy image
    imagedestroy($image);
}

function capya_string($len){
    $str='';
    for($i=1; $i<=$len; $i++) {
        $ord=rand(48, 90);
        if((($ord >= 48) && ($ord <= 57)) || (($ord >= 65) && ($ord<= 90))) $str.=chr($ord);
        else $str.=capya_string(1);
    }
    return $str;
}
?>

PHP:
<html>
<head>
<script language="JavaScript" type="text/javascript" src="ajax_captcha.js"></script>
</head>
<body>
Contact Us</font></h1>
<p><em>Fields marked with <strong>*</strong> are required.</em></p>
<form name="form_contact" id="form_contact" method="post" action="contact.php">

<?php showSet("name"); ?>
<?php showSet("contact"); ?>
<?php showSet("profession"); ?>
<?php showSet("location"); ?>
<?php showSet("info"); ?>
<?php showSet("preferences"); ?>

<!-- Url of your captcha-script here -->
<img src="http://forums.x10hosting.com/programming-help/create_image.php" alt="captcha" />
<input id="txtCaptcha" type="text" name="txtCaptcha" value="" maxlength="10" size="32" onchange="getParam(document.frmCaptcha)" />

<div class="buttons"><button type="submit" class="positive" id="sendme" title="Send your message" onclick="getParam(document.form_contact)" />
<img src="http://forums.x10hosting.com/programming-help/email_go.gif" alt="Send!"> 
Send!</button>
</div>
</form>
</body>
</html>

I suppose you're able to get your javascript to work yourself? And by the way, don't rely on client-side checking of the captcha, always check it again server-side!
 
Last edited:

klnce

New Member
Messages
35
Reaction score
0
Points
0
I personally use the captcha from this site

http://phpcaptcha.org/latest/

It has AJAX captcha so an alert displays without reloading the page. And this captcha has some inbuilt work done, avoiding some confusion between 0 and o , 1 and l . s and 5......... It has answer for every one.


Please don't visit my site http://klnce.elementfx.com
 
Top