Php-implemented click captcha click Verification Code class instance, clickcaptcha
This example describes the click captcha Verification Code class implemented by php and its usage. It is a very practical function. Share it with you for your reference. The details are as follows:
I. Requirements:
Currently, most common form verification codes require user input, which is inconvenient for mobile users.
If a mobile phone user accesses the website, the verification code can be confirmed by clicking a location, which is much more convenient.
Ii. Principles:
1. Create a PNG Image Using PHP imagecreate, draw N arcs in the graph, one of which is a complete circle (used for verification), and record the coordinates and radius of the center of the circle into the session.
2. In the browser, when you click on the Verification Code image, record the click position.
3. Compare the coordinates clicked by the user with the Center Coordinate and radius of the session record to determine whether they are in the circle. If so, the verification passes.
Shows the program running effect:
Iii. Implementation Method:
The ClickCaptcha. class. php class file is as follows:
<? Php/** Click Captcha Verification 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 */public function create () {// create an image $ this-> _ img_res = imagecreate ($ this-> width, $ this-> height); // fill in the 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-> I ConColor [2]); $ minArea = $ this-> iconSize/2 + 3; // obfuscated image, incomplete circle 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 the image with the complete circle $ x = mt_rand ($ minArea, $ this-> width-$ minArea); $ y = mt_rand ($ minAre A, $ this-> height-$ minArea); $ r = $ this-> iconSize/2; imagearc ($ this-> _ img_res, $ x, $ y, $ this-> iconSize, $ this-> iconSize, 0,360, $ col_ellipse); // records the Center Coordinate and radius $ this-> captcha_session ($ this-> sess_name, array ($ x, $ y, $ r); // generates the image Header ("Content-type: image/PNG"); ImagePNG ($ this-> _ img_res ); imageDestroy ($ this-> _ img_res); exit ();}/** check the Verification Code * @ param String $ captcha Verification Code * @ param int $0 after the flag verification is successful: do not clear ses Sion 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) & 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 ;} /** determine whether the vertex is in the circle * @ param int $ px point x * @ param int $ py point y * @ param int $ cx center x * @ param int $ cy Center y * @ param 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 nam E * @ 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">
Click here to download the complete source code for this article.
I hope this article will help you with PHP programming.
The verification code on the php registration page is as follows:
You need to determine whether the verification code you entered is consistent with the one you saved earlier.
If ($ _ POST ["captcha"]! = $ _ SESSION ["captcha"])
{
Echo "Incorrect verification code ";
}
How can I change the verification code generated by Zend_Captcha? -PHP framework development
If you mean the effect of Ajax, you can use a method like this: $ captcha = new Zend_Captcha_Image (array (......)); $ id = $ captcha-> generate (); echo \'\'. $ captcha-> render ($ view ). \'\';//. jsfunction changeCaptcha (){? ???......? ??? Document. getElementById (\ 'captcha \ '). innerHtml = ......}