Jump to content

insertion of an php-captcha-script into a guestbook-script


frajofu

Recommended Posts

Hello,

 

I've got a php-guestbook since half a year running, and now I get a lot of spam-messages there.

I would like to insert a functional captcha-script to the message-setup-function of the guestbook-script.

Who would like to help me?

I can send the files for have a look..

 

Link to comment
Share on other sites

Have you tried using google ???

And don't forget, THE MANUAL IS YOUR FRIEND !

 

First off you need to create a random string, this can be done in sooooo many different ways!

 

$string = substr(md5(rand(0,1000)),0,5);

 

If you don't know, this will create a random number, then encrypt it and then take the first 5 characters (substr, md5, rand)

 

Next you will need to get an image and set it for the actual captcha image (if that makes sense)

 

$image_link = "directory/folder/file.gif";

$image = imagecreatefromgif($image_link);

 

For this your image needs to be about 90 x 30 (imagecreatefromgif)

 

Next you need to give the text a colour

 

$string_colour = imagecolorallocate($image, 255, 45, 45);

 

This is nearly a perfect red (imagecolorallocate)

 

Now, we have to put it all together and set the string to a session.

 

imagestring($image, 5, 20, 10, $string, $string_colour);

session_start();

$_SESSION['key'] = md5($string);

 

The numbers are the font-szie and the co-ordinates (imagestring, sessions, session_start)

 

Lastly, we have to output the image to the browser.

 

header("Content-type: image/gif");

imagegif($image);

 

NOTE: You can not have anything else on this page (headers, imagegif)

 

Now you have the captcha created you need to save this as captcha.php, then you just need to include it in your page and check the users code meets the text in the captcha.

 

<img src="captcha.php">

 

To stop it caching I recommend adding a random string on the end, so you now have

 

$md5 = md5(rand(1,1000));

echo "<img src=\"captcha.php?{$md5}\">";

 

Now for the form.

 

<?php
session_start();
$key = $_SESSION['key'];
$text = md5($_POST['text']);
?>
<form name="captcha" method="post" action="<?php echo $PHP_SELF; ?>">
<input type="text" name="text">
<br>
<input type="submit" name="submit">
</form>
<?php
if($_POST)
{
     if($text != $key)
     {
          die("Incorrect Captcha");
     } else
          if($text == $key)
          {
               echo "Captcha Correct";
          }
}
?>

 

Now I think thats everything ;D This is only a very basic captcha and you probably should modify it, by adding lines, changing font, rotation, colour, string length etc

 

Anyways, I hope it helps ;D

 

~ Chocopi

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.