Php-implemented clickcaptcha click verification code class instance

Source: Internet
Author: User
This article mainly introduces the clickcaptcha click verification code instance implemented by php. unlike traditional verification codes, this verification code class is very practical for mobile users to click a location to confirm the verification code, you can refer to the example in this article to describe the click captcha click verification code class implemented by php and its usage, which 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)
  
 

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 'back';} else {// html?>     
   Click Captcha Demo  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.