Selasa, 20 September 2011

Make Form and Simple Captcha Code in PHP

Now i want to share about how to make simple form and Captcha use PHP for your website but what for is Captcha actually ? wait a minute, i'm searching it....^_^
Captcha is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a person. hmm... i doubt about this, do you think animal or tree can make Facebook account ? hhahaha...
this is what i found in Wikipedia.



A CAPTCHA is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a person. The process usually involves one computer (a server) asking a user to complete a simple test which the computer is able to generate and grade. Because other computers are assumed to be unable to solve the CAPTCHA, any user entering a correct solution is presumed to be human. Thus, it is sometimes described as a reverse Turing test, because it is administered by a machine and targeted to a human, in contrast to the standard Turing test that is typically administered by a human and targeted to a machine. A common type of CAPTCHA requires the user to type letters or digits from a distorted image that appears on the screen.
The term "CAPTCHA" was coined in 2000 by Luis von Ahn, Manuel Blum, Nicholas J. Hopper, and John Langford (all of Carnegie Mellon University). It is an acronym based on the word "capture" and standing for "Completely Automated Public Turing test to tell Computers and Humans Apart". Carnegie Mellon University attempted to trademark the term, but the trademark application was abandoned on 21 April 2008.
Characteristics: A CAPTCHA is a means of automatically generating challenges which intends to:

  • Provide a problem easy enough for all humans to solve.
  • Prevent standard automated software from filling out a form
A check box in a form that reads "check this box please" is the simplest (and perhaps least effective) form of a CAPTCHA. CAPTCHAs do not have to rely on difficult problems in artificial intelligence, although they can.
This has the benefit of distinguishing humans from computers. It also creates incentive to further develop artificial intelligence of computers.

Do you get it ? ohh, it's okay take it easy...^_^
Now for tutorial
In this tutorial we use 2 script file and 1 image file, first make new file and add this following code

<?php
$imgcode = "verification.png";
$rgb = array(255, 0, 0);
header("content-type: image/png");
$string=substr(md5($_GET[time]),-5);
$im = imageCreateFromPng($imgcode);
$mycolor = ImageColorAllocate($im, $rgb[0], $rgb[1], $rgb[2]);
ImageString($im,16,10,2,$string,$mycolor);
ImagePng($im);
ImageDestroy($im);
?>
look "verification.png", that is a background image for your Captcha code, change as you wish and upload your background image to your hosting. Don't forget save as captcha.php.
Make new files again for form code, add this following code

<?php
$time
= isset($_REQUEST['time']) ? $_REQUEST['time'] : ''; $code = isset($_REQUEST['code']) ? $_REQUEST['code'] : '';
if ((isset(
$_REQUEST['submit'])) && ($_SERVER['REQUEST_METHOD']=='POST')) {
if ((
substr(md5($time),-5) != $code)) {
die (
'Code salah. <a href="/form.php">back</a>');
} else {
?> <script type="text/javascript" language="javascript">
window.alert('Chapcha sukses dengan kode <?=(substr(md5($_POST['time']),-5))?>');
</script>
<?php
}
}
?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="time" value="<?=time()?>" />
<img src="/captcha.php?time=<?=time()?>" border="0" width="60" height="20" />
<div style="font-size:small">Verification code</div>
<div><input type="text" name="code" maxlength="5" size="5" /></div>
<input type="submit" name="submit" value="Submit" />
</form>


Save as form.php, and here we go congratulation, i can't believe it you can do it...
you're the man...^_^

Quotes for this post

Do it now what you can do now

0 komentar:

Posting Komentar

Lebih baik diam daripada tidak bisa bicara baik, (Silent is gold)