thinkphp系列之--极验滑动验证码

发布时间:1970-01-01  编辑:Mrs.默先森 

    对于建站的童鞋来说;恶意广告是防不胜防;自己搞个验证码吧,又各种被吐槽;小编发现了这个极验验证码,一定能帮助各位筒子解决这个问题;这绝对一个体验好安全高的方案;极验验证码可以帮助您搭建优雅的验证安全系统官网:http://www.geetest.com;速速来体验一番这个安全智能的验证系统,让我也来引领验证码新时代吧^_^

    首先你的模板文件可以这样来

    <head>
        <meta charset="UTF-8">
        <title>极验证码</title>
        <!--载入bootstrap css-->
        <link rel="stylesheet" href="/static/node_modules/bootstrap3/dist/css/bootstrap.min.css">
        <!--载入jquery-->
        <script src="/static/node_modules/jquery/dist/jquery.min.js"></script>
        <!--载入layer js-->
        <script src="/static/layer-v3.0.3/layer/layer.js"></script></head><body>
        <div class="container">
            <div class="row">
                <form action="" method="POST" role="form" class="col-lg-4">
                	<legend>极验证</legend>
                    <div class="form-group">
                        <label for="">用户名</label>
                        <input type="text" class="form-control" name="username" value="wubin" id="" placeholder="">
                    </div>
                    <div class="form-group">
                        <label for="">密码</label>
                        <input type="text" class="form-control" name="password"  value="wubin" placeholder="">
                    </div>
                    <div class="form-group" id="embed-captcha">
                        <label id='wait' for="">正在加载验证码</label>
                    </div>
                    <button type="submit" id="embed-submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
        <script src="/static/geetest/gt.js"></script>
        <script>
            var handlerEmbed = function (captchaObj) {
                $("#embed-submit").click(function (e) {
                    var validate = captchaObj.getValidate();
                    if (!validate) {
                        //layer弹出层
                        layer.msg('请先完成验证。。。', {icon: 5});
                        e.preventDefault();
                    }
                });
                // 将验证码加到id为captcha的元素里,同时会有三个input的值:geetest_challenge, geetest_validate, geetest_seccode
                captchaObj.appendTo("#embed-captcha");
                captchaObj.onReady(function () {
                    $("#wait").hide();
                });
                // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
            };
            $.ajax({
                // 获取id,challenge,success(是否启用failback)
                url: "{:url('geetest/geetest/StartCaptchaServlet')}?t=" + (new Date()).getTime(), // 加随机数防止缓存
                type: "get",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    // 使用initGeetest接口
                    // 参数1:配置参数
                    // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
                    initGeetest({
                        gt: data.gt,
                        challenge: data.challenge,
                        new_captcha: data.new_captcha,
                        product: "embed", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
                        offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
                        // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config
                    }, handlerEmbed);
                }
            });
        </script>
    </body>

    随后需要在$.ajax中url指向的请求地址(StartCaptchaServlet)中:

    public function StartCaptchaServlet()
    	{
    		$GtSdk = new GeetestLib('6fca74f2cb439f6318e83979cc8f642d','f1b617c21c0f513e1f1a9938ffe24e5a');
    		$data = array(
    			"user_id" => "test", # 网站用户id
    			"client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
    			"ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP
    		);
    		$status = $GtSdk->pre_process($data, 1);
    		$_SESSION['gtserver'] = $status;
    		$_SESSION['user_id'] = 1;
    		echo $GtSdk->get_response_str();
    	}

    这个时候你还需要一个GeetestLib类:

    captcha_id  = $captcha_id;
    		$this->private_key = $private_key;
    	}
    
    	/**
    	 * 判断极验服务器是否down机
    	 *
    	 * @param array $data
    	 * @return int
    	 */
    	public function pre_process($param, $new_captcha=1) {
    		$data = array('gt'=>$this->captcha_id,
    					  'new_captcha'=>$new_captcha
    		);
    		$data = array_merge($data,$param);
    		$query = http_build_query($data);
    		$url = "http://api.geetest.com/register.php?" . $query;
    		$challenge = $this->send_request($url);
    		if (strlen($challenge) != 32) {
    			$this->failback_process();
    			return 0;
    		}
    		$this->success_process($challenge);
    		return 1;
    	}
    
    	/**
    	 * @param $challenge
    	 */
    	private function success_process($challenge) {
    		$challenge      = md5($challenge . $this->private_key);
    		$result         = array(
    			'success'   => 1,
    			'gt'        => $this->captcha_id,
    			'challenge' => $challenge,
    			'new_captcha'=>1
    		);
    		$this->response = $result;
    	}
    
    	/**
    	 *
    	 */
    	private function failback_process() {
    		$rnd1           = md5(rand(0, 100));
    		$rnd2           = md5(rand(0, 100));
    		$challenge      = $rnd1 . substr($rnd2, 0, 2);
    		$result         = array(
    			'success'   => 0,
    			'gt'        => $this->captcha_id,
    			'challenge' => $challenge,
    			'new_captcha'=>1
    		);
    		$this->response = $result;
    	}
    
    	/**
    	 * @return mixed
    	 */
    	public function get_response_str() {
    		return json_encode($this->response);
    	}
    
    	/**
    	 * 返回数组方便扩展
    	 *
    	 * @return mixed
    	 */
    	public function get_response() {
    		return $this->response;
    	}
    
    	/**
    	 * 正常模式获取验证结果
    	 *
    	 * @param string $challenge
    	 * @param string $validate
    	 * @param string $seccode
    	 * @param array $param
    	 * @return int
    	 */
    	public function success_validate($challenge, $validate, $seccode,$param, $json_format=1) {
    		if (!$this->check_validate($challenge, $validate)) {
    			return 0;
    		}
    		$query = array(
    			"seccode" => $seccode,
    			"timestamp"=>time(),
    			"challenge"=>$challenge,
    			"captchaid"=>$this->captcha_id,
    			"json_format"=>$json_format,
    			"sdk"     => self::GT_SDK_VERSION
    		);
    		$query = array_merge($query,$param);
    		$url          = "http://api.geetest.com/validate.php";
    		$codevalidate = $this->post_request($url, $query);
    		$obj = json_decode($codevalidate,true);
    		if ($obj === false){
    			return 0;
    		}
    		if ($obj['seccode'] == md5($seccode)) {
    			return 1;
    		} else {
    			return 0;
    		}
    	}
    
    	/**
    	 * 宕机模式获取验证结果
    	 *
    	 * @param $challenge
    	 * @param $validate
    	 * @param $seccode
    	 * @return int
    	 */
    	public function fail_validate($challenge, $validate, $seccode) {
    		if(md5($challenge) == $validate){
    			return 1;
    		}else{
    			return 0;
    		}
    	}
    
    	/**
    	 * @param $challenge
    	 * @param $validate
    	 * @return bool
    	 */
    	private function check_validate($challenge, $validate) {
    		if (strlen($validate) != 32) {
    			return false;
    		}
    		if (md5($this->private_key . 'geetest' . $challenge) != $validate) {
    			return false;
    		}
    
    		return true;
    	}
    
    	/**
    	 * GET 请求
    	 *
    	 * @param $url
    	 * @return mixed|string
    	 */
    	private function send_request($url) {
    
    		if (function_exists('curl_exec')) {
    			$ch = curl_init();
    			curl_setopt($ch, CURLOPT_URL, $url);
    			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
    			curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    			$curl_errno = curl_errno($ch);
    			$data = curl_exec($ch);
    			curl_close($ch);
    			if ($curl_errno >0) {
    				return 0;
    			}else{
    				return $data;
    			}
    		} else {
    			$opts    = array(
    				'http' => array(
    					'method'  => "GET",
    					'timeout' => self::$connectTimeout + self::$socketTimeout,
    				)
    			);
    			$context = stream_context_create($opts);
    			$data    = @file_get_contents($url, false, $context);
    			if($data){
    				return $data;
    			}else{
    				return 0;
    			}
    		}
    	}
    
    	/**
    	 *
    	 * @param       $url
    	 * @param array $postdata
    	 * @return mixed|string
    	 */
    	private function post_request($url, $postdata = '') {
    		if (!$postdata) {
    			return false;
    		}
    
    		$data = http_build_query($postdata);
    		if (function_exists('curl_exec')) {
    			$ch = curl_init();
    			curl_setopt($ch, CURLOPT_URL, $url);
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
    			curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
    
    			//不可能执行到的代码
    			if (!$postdata) {
    				curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    			} else {
    				curl_setopt($ch, CURLOPT_POST, 1);
    				curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    			}
    			$data = curl_exec($ch);
    
    			if (curl_errno($ch)) {
    				$err = sprintf("curl[%s] error[%s]", $url, curl_errno($ch) . ':' . curl_error($ch));
    				$this->triggerError($err);
    			}
    
    			curl_close($ch);
    		} else {
    			if ($postdata) {
    				$opts    = array(
    					'http' => array(
    						'method'  => 'POST',
    						'header'  => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
    						'content' => $data,
    						'timeout' => self::$connectTimeout + self::$socketTimeout
    					)
    				);
    				$context = stream_context_create($opts);
    				$data    = file_get_contents($url, false, $context);
    			}
    		}
    
    		return $data;
    	}
    
    
    
    	/**
    	 * @param $err
    	 */
    	private function triggerError($err) {
    		trigger_error($err);
    	}
    }

    标签php


本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。

陶太富博客 http://blog.taotaifu.cn

最新发布

最新评论

0.267331s