Jump to content

need easy to use captcha script


rhock_95

Recommended Posts

I hope someone can help me..,

 

I am looking for a complete captcha script that actually works...I have tried just about every one I can by searching and none of them work out of the box...

 

I either get "header already sent..."errors  OR the ones that "seem" to work DO NOT display the text (font) over top of the background image...

 

GD Support  enabled  
GD Version  bundled (2.0.15 compatible)  
FreeType Support  enabled  
FreeType Linkage  with TTF library  
GIF Read Support  enabled  
JPG Support  enabled  
PNG Support  enabled  
WBMP Support  enabled  
XBM Support  enabled  

 

can anyond suggest a foolproof script?

Link to comment
Share on other sites

I think the previously mentioned problem is du to an older version of PHP...

I installed the script on a newer server (PHP 5.04) and the test page seems to work

 

however I need some advice on how to implement the security function on an existing form

 

here is the test page:

<?php
//error_reporting(E_ALL);
session_start();
if (isset($_POST['submit'])) {
// clean and check form inputs including the secure image code
    $name = trim(strip_tags($_POST['name']));
    $email = trim(strip_tags($_POST['email']));
    $comments = trim(strip_tags($_POST['comments']));
    $secure = strtoupper(trim(strip_tags($_POST['secure'])));
    $match = $_SESSION['captcha']; // the code on the image

// input error checking
    if ($name=="") {
        $err.= "Please provide your name<br/>";
    }
    if (!$email) {
        $err.= "Please provide your email address<br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err.= $email. " is not a valid email address.<br/>";
        }
    } 
    if ($comments=="") {
        $err.= "Please provide comments<br/>";
    }
    if (!$secure) {
        $err.= "No security code entered<br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err.= "Security code mismatch<br/>";
    }
    if ($err=="") {
    // success - input passed all tests
    echo "What you do with success is up to you.";
    exit();
    }
}
?>
<head>
<title>Trolls go away</title>
<style type="text/css">
body,td {
font-family:arial, helvetica, sans-serif;
background:#fff;
color:#000;
font-size:12px;
}
input, textarea {
background:#eee;
color:#000;
font-size:12px;
border:1px solid #000;   
}
</style>
</head>
<body>
<?php
if ($err!="") {
    echo "<strong>Form Error(s)</strong><br/>";
    echo "<font color='#cc3300'>". nl2br($err). "</font><br/>";
}
?>

<form name="captcha" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table cellpadding="3" cellspacing="2" style="border:1px dotted #666;">
<tr>
<td>Name:</td><td><input type="text" name="name" value="<?php echo $_POST['name'];?>"/></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" value="<?php echo $_POST['email'];?>"/></td>
</tr>
<tr>
<td valign="top">Comments:</td><td><textarea rows="5" columns="30" name="comments"><?php echo $_POST['comments'];?></textarea></td>
</tr>
<tr>
<td>Security Code</td><td><input type="text" name="secure"/></td>
</tr>
<tr>
<td><img src="captcha_image.php" alt="security image" border="0"/></td><td><input type="submit" name="submit" value="Send"/></td>
</tr>
</table>
</form>
</body>
</html>

 

I need to use the security function on an existing form that calls a different php script (that searches a database)

Link to comment
Share on other sites

Here is the code for my capthca image I use saved as captcha .php

 

<?php
session_start();
$strlength = rand(4,7);
$captchastr = "";
for($i=1;$i<=$strlength;$i++)
{
$textornumber = rand(1,3);
if($textornumber == 1)
{
$captchastr .= chr(rand(49,57));
}
if($textornumber == 2)
{
$captchastr .= chr(rand(65,78));
}
if($textornumber == 3)
{
$captchastr .= chr(rand(80,90));
}
}
$randcolR = rand(100,230);
$randcolG = rand(100,230);
$randcolB = rand(100,230);

//initialize image $captcha is handle dimensions 200,50
$captcha = imageCreate(190,50);
$backcolor = imageColorAllocate($captcha, $randcolR, $randcolG, $randcolB);

$txtcolor = imageColorAllocate($captcha, ($randcolR - 60), ($randcolG - 60), ($randcolB - 60));
for($i=1;$i<=$strlength;$i++)
{

$clockorcounter = rand(1,2);
if ($clockorcounter == 1)
{
$rotangle = rand(0,45);
}
if ($clockorcounter == 2)
{
$rotangle = rand(315,360);
}

//$i*25 spaces the characters 25 pixels apart
imagettftext($captcha,rand(14,20),$rotangle,($i*25),30,$txtcolor,"/arial.ttf",substr($captchastr,($i-1),1));
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$txtcolor);
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$backcolor);
}
//Send the headers (at last possible time)
header('Content-type: image/png');

//Output the image as a PNG
imagePNG($captcha);

//Delete the image from memory
imageDestroy($captcha);

$_SESSION["captchastr"] = $captchastr;
?>

 

You neeed <?php session_start(); ?> at the beginning of the page where you are calling the capthca image

 

to display the capthca image you use

<img src="captcha.php" alt="security image" />

 

and i use something like this to check to see if the text field is empty of if the text matches the image

if (empty($_POST['verify']) && $_POST['verify'] == $_SESSION['captchstr'])
{
    $errmsg.="Please enter Anti-Spam key:<br />";
    $valid=false;
}

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.