PHP - Simple PHP captcha script
페이지 정보
작성자 휴먼 댓글 0건 조회 8,343회 작성일 14-07-14 22:50본문
PHP - Simple PHP captcha script
A very basic PHP / GD based captcha script for stopping those spammers from flooding your guestbooks, mailforms or any other online application susceptible to spam spawning web bots.
Requires only PHP with GD library installation and a true type font file(.ttf) to create the image
First you make the actual captcha image generating file, and save it as (for instance) 'captcha.php':
To see an example of this script in a live site - have a look at this guestbook. Or just leave a comment below this article
<?php session_start(); // generate random number and store in session $randomnr = rand(1000, 9999); $_SESSION['randomnr2'] = md5($randomnr); //generate image $im = imagecreatetruecolor(100, 38); //colors: $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 200, 35, $black); //path to font: $font = '/var/www/font.ttf'; //draw text: imagettftext($im, 35, 0, 22, 24, $grey, $font, $randomnr); imagettftext($im, 35, 0, 15, 26, $white, $font, $randomnr); // prevent client side caching header("Expires: Wed, 1 Jan 1997 00:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); //send image to browser header ("Content-type: image/gif"); imagegif($im); imagedestroy($im); ?>
this is an exampe of a form containing the captcha image file captcha.php:
<html> <head> <title>html form with php captcha</title> </head> <body> <form method="post" action="write.php"> <input class="input" type="text" name="norobot" /> <img src="captcha.php" /> <input type="submit" value="Submit" /> </form> </body> </html>
Now all we need is a serverside file that processes the values sent by this form and checks if the captcha test was passed succesfully. In the form above this is refered to by the attribute "action=write.php" and it should contain code something similar to the following:
<?php session_start(); if (md5($_POST['norobot']) == $_SESSION['randomnr2']) { // here you place code to be executed if the captcha test passes echo "Hey great , it appears you are not a robot"; } else { // here you place code to be executed if the captcha test fails echo "you're a very naughty robot!"; } ?>
관련링크
댓글목록
등록된 댓글이 없습니다.