This article describes the PHP implementation of click CAPTCHA Click Verification Code class and its usage, is a very practical function. Share for everyone to use for reference. Specifically as follows:
First, the demand:
The most commonly used form verification code is to user input mainly, but this will be inconvenient for mobile phone users.
If the mobile phone user access, you can not input, but click a location can confirm the verification code, so it will be more convenient.
Second, the principle:
1. Create a PNG image using PHP imagecreate, Draw n arcs in the diagram, one of which is a complete circle (validated), and record the center coordinates and radii into session.
2. In the browser, when the user clicks on the Verification code picture, record the location of the click.
3. The user clicks the coordinates and the session record center coordinates, the radius comparison, determines whether in the circle, if then verifies passes.
The program works as shown in the following illustration:
Third, the realization method:
The ClickCaptcha.class.php class files are as follows:
<?php/** Click Captcha Authentication Code class * DATE:2013-05-04 * author:fdipzone * ver:1.0 */class Clickcaptcha {//
Class Start public $sess _name = ' M_captcha ';
Public $width = 500;
Public $height = 200;
Public $icon = 5;
Public $iconColor = Array (255, 255, 0);
Public $backgroundColor = Array (0, 0, 0);
Public $iconSize = 56;
Private $_img_res = null;
Public function __construct ($sess _name= ') {if (session_id () = = ') {session_start (); } if ($sess _name!= ') {$this->sess_name = $sess _name;//Set Session name}}/** Create verification code */P
ublic function Create () {//create image $this->_img_res = imagecreate ($this->width, $this->height); Fill Background imagecolorallocate ($this->_img_res, $this->backgroundcolor[0], $this->backgroundcolor[1], $this-
>BACKGROUNDCOLOR[2]); Assign color $col _ellipse = imagecolorallocate ($this->_img_res, $this->iconcolor[0], $this->iconcoloR[1], $this->iconcolor[2]);
$minArea = $this->iconsize/2+3;
Confusing with images, incomplete round for ($i =0; $i < $this->icon; $i + +) {$x = Mt_rand ($minArea, $this->width-$minArea);
$y = Mt_rand ($minArea, $this->height-$minArea);
$s = Mt_rand (0, 360);
$e = $s + 330;
Imagearc ($this->_img_res, $x, $y, $this->iconsize, $this->iconsize, $s, $e, $col _ellipse);
}//Verify with image, complete circle $x = Mt_rand ($minArea, $this->width-$minArea);
$y = Mt_rand ($minArea, $this->height-$minArea);
$r = $this->iconsize/2;
Imagearc ($this->_img_res, $x, $y, $this->iconsize, $this->iconsize, 0, 360, $col _ellipse);
Record center coordinates and radii $this->captcha_session ($this->sess_name, Array ($x, $y, $r));
Generate Image Header ("Content-type:image/png");
Imagepng ($this->_img_res);
Imagedestroy ($this->_img_res);
Exit (); /** Check Verification code * @param String $captcha Verification code * @param inT $flag validation succeeds 0: Do not clear session 1: Clear Session * @return Boolean/Public function check ($captcha, $flag =1) {if (
Trim ($captcha) = = ') {return false;
} if (!is_array ($this->captcha_session ($this->sess_name)) {return false;
List ($px, $py) = Explode (', ', $captcha);
List ($CX, $cy, $CR) = $this->captcha_session ($this->sess_name); if (Isset ($px) && is_numeric ($px) && isset ($py) && is_numeric ($py) && isset ($CX) &A mp;& is_numeric ($CX) && isset ($cy) && is_numeric ($cy) && isset ($CR) && is_numeric ($ CR)) {if ($this->pointinarea ($px, $py, $cx, $cy, $CR)) {if ($flag ==1) {$this->captcha_session ($
This->sess_name, "");
return true;
return false; /** determines whether the point is in the circle * @param int $px dot x * @param int $py point y * @param int $cx Center x * @param int $cy Center Y * @pa
Ram int $CR Circle radius * SQRT (x^2+y^2) <r/Private Function Pointinarea ($px, $py, $cx, $cy, $CR) {$x = $cx-$px;
$y = $cy-$py;
Return round (sqrt ($x * $x + $y * $y)) < $CR;
/** Verification Code Session processing method * @param string $name captcha Session name * @param string $value * @return string * * Private Function captcha_session ($name, $value =null) {if (Isset ($value)) {if ($value!== ') {$_
session[$name] = $value;
}else{unset ($_session[$name]);
}else{return Isset ($_session[$name])? $_session[$name]: ';
}}//class end?>
The
demo.php sample program is as follows:
<?php session_start ();
Require (' ClickCaptcha.class.php ');
if (Isset ($_get[' Get_captcha ')) {//get captcha $obj = new Clickcaptcha ();
$obj->create ();
Exit (); } if (isset $_post[' send ') && $_post[' send ']== ' true ') {//Submit $name = Isset ($_post[' name '])? Trim ($_post[
' Name ']: '; $captcha = isset ($_post[' captcha '))?
Trim ($_post[' captcha ']): ';
$obj = new Clickcaptcha ();
if ($obj->check ($captcha)) {echo ' Your name is: '. $name;
}else{Echo ' captcha not match ';
Echo ' <a href= ' demo.php ' >back</a> '; }else{//HTML?> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
This article full source click here to download the site.
I hope this article will help you with your PHP program design.