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.
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.
Principle:
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.
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-> iconColor [2]); $ MinArea = $ this-> iconSize/2 + 3; // obfuscation 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, complete circle $ x = mt_rand ($ minArea, $ this-> width-$ minArea); $ y = mt_rand ($ minArea, $ this-> hei Ght-$ minArea); $ r = $ this-> iconSize/2; imagearc ($ this-> _ img_res, $ x, $ y, $ this-> iconSize, $ this-> iconSize, 0,360, $ col_ellipse); // record the center coordinate and radius $ this-> captcha_session ($ this-> sess_name, array ($ x, $ y, $ r); // generate 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 session 1: clear sess Ion * @ 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)
Demo. php
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