PHP code example of qqfirst / geetest

1. Go to this page and download the library: Download qqfirst/geetest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

qqfirst / geetest example snippets


public function geetestInit(Request $request)
{
    /**
     * @var
     * GeetestMain
     */
    $c = Container::make(GeetestMain::class, [Env::get('geetest.id'), Env::get('geetest.key')]);
    $challenge=$c->register($request->getRealIp(true));
    //记录到用户,本次验证的geet服务器状态。
    $request->session()->set('geetest_server_status', $c->getPassby());
    return json([
        'code' => 0,
        'geetest_id'=>Env::get('geetest.id'),
        'geetest_status'=>$c->getPassby()?1:0,
        'challenge' => $challenge
    ]);
}

public function geetestValidate(Request $request)
{
    /**
     * @var
     * GeetestMain
     */
    $c = Container::make(GeetestMain::class, [Env::get('geetest.id'), Env::get('geetest.key')]);

    $seccode=$request->post('geetest_seccode');
    $validate=$request->post('geetest_validate');
    $challenge=$request->post('geetest_challenge');


    $geet_status=$request->session->get('geetest_server_status',false);
    $seccode = $c->validate($geet_status,$validate,$seccode,$challenge,$request->getRealIp(true));

    if(strtolower(strval($seccode))=='false'){
        return  json([
            'code'=>1,
            'msg'=>'验证未通过,请重新验证。'
        ]);
    }
    $request->session()->set('geetest_status',true);
    return json([
        'code' => 0,
        'seccode' =>$seccode
    ]);
}