frajofu Posted August 15, 2007 Share Posted August 15, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/65021-insertion-of-an-php-captcha-script-into-a-guestbook-script/ Share on other sites More sharing options...
chocopi Posted August 15, 2007 Share Posted August 15, 2007 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 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 ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/65021-insertion-of-an-php-captcha-script-into-a-guestbook-script/#findComment-324570 Share on other sites More sharing options...
phpknight Posted August 15, 2007 Share Posted August 15, 2007 Also, PEAR has a Text_CAPTCHA class that is very easy to implement. The book, _Foundations of PEAR_ has a nice example. Quote Link to comment https://forums.phpfreaks.com/topic/65021-insertion-of-an-php-captcha-script-into-a-guestbook-script/#findComment-324578 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.