CodeIgniter reCaptcha 问题

发布于 2024-10-31 02:54:13 字数 831 浏览 1 评论 0原文

好吧,我在这个网站上看到过类似的问题,但他们没有我在这个特定问题上寻找的所有手持细节!

我在使用 reCaptcha 的 JS 版本和为 CodeIgniter 1.7.1 制作的 PHP 版本之间徘徊。

使用 JS 版本,我将其显示在生成的 HTML 中,但我不确定如何将正确的结果传递给 PHP形式。目前,周围的表单仅在 PHP 中发布是否有“任何”文本输入,而不是是否是正确的文本。我可以监视验证码文本输入字段以查看是否提交了任何内容,但这不会检查实际的 javascript 以获得正确的结果。我需要一个 PHP 函数调用,可以从 javascript 使用 Recaptcha.create 生成的 HTML 中获取调用。

您可能会想:既然 codeigniter 是 PHP 的,为什么不直接使用 PHP 版本的 recaptcha 呢? CodeIgniter 结构是我处理过的最不直观的 PHP 和 MVC 结构。在我正在编辑的这个网站上查找必要的函数调用已经变成了逆向工程练习。

我还有一个 PHP 版本,在这里制作 https://github.com/seanmcgary/Codeigniter-ReCaptcha,就像经典的 CodeIgniter 风格一样,文档基本上不存在。我知道该版本适用于 CodeIgniter 1.7,但我不知道它是什么版本的 reCaptcha。

请不要将我链接到 codeigniter wiki,仅提供相关帮助。

实现 JS 版本和部分 PHP 版本变得很复杂,因为一个版本可能更实用。所以想请教一下各位,谢谢!

Okay all, I have seen similar questions asked on this site, but they don't have all the handholding details I'm looking for on this particular issue!

I am in limbo between using the JS version of reCaptcha and a PHP version made for CodeIgniter 1.7.1

With the JS version I have it displayed within the generated HTML but I'm not sure how to get the proper result passed through to the PHP form. The surrounding form currently only posts in the PHP whether there was "any" text input, not if it was proper text. I can monitor the captcha text input field to see if anything was submitted, but this doesn't check the actual javascript for a proper result. I need a PHP function call that can get a call from the HTML that the javascript generates with Recaptcha.create.

You might be thinking: Since codeigniter is PHP why not just use the PHP version of recaptcha? Well the CodeIgniter structure is the most unintuitive PHP and MVC structure I've dealt with. Finding the necessary function calls on this site I am editing has turned into a reverse engineering exercise.

I also have a PHP version, made here https://github.com/seanmcgary/Codeigniter-ReCaptcha, and just like in classic CodeIgniter flavor, the documentation is basically nonexistent. I know that version is for CodeIgniter 1.7, but I don't know what version of reCaptcha it is.

Please don't link me to the codeigniter wiki, just relevant assistance.

It is becoming convoluted to work on implementing the JS version, and parts of a PHP version because one version might be more practical. So I'm asking you guys, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭泣的笑容 2024-11-07 02:54:13

我有我的想法 recaptcha 如何在 Codigniter 上工作。我无法清楚地向您解释,但我希望您能理解下面的代码:

将此代码添加到您的配置文件中

#google reCaptcha
$config['recaptcha_sitekey'] = 'put site key'; //Recaptcha Site key
$config['recaptcha_secretkey'] = 'put secret key'; //Recaptcha Secret Key

将此代码添加到您的验证表单中

$this->form_validation->set_rules('g-recaptcha-response', 'recaptcha validation', 'required|callback_validate_captcha');
        $this->form_validation->set_message('validate_captcha', 'Please check the the captcha form');

,并且不要忘记将此代码添加到 recaptcha 将验证的控制器中

//ADDITIONAL CODES
    function validate_captcha() {
        $captcha = $this->input->post('g-recaptcha-response');
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$this->config->item('recaptcha_sitekey')."&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);

        if ($response . 'success' == false) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

将此代码添加到表单中哪里会出现 recaptcha

 <div class="g-recaptcha" data-sitekey="<?php echo $this->config->item('recaptcha_sitekey') ?>"></div> 

并且不要忘记 Javascript

<script src="https://www.google.com/recaptcha/api.js"></script>

如果您想从主页到模式中出现两次 recaptcha 将此代码添加到您的模式中

<script>
    createRecaptcha();
    function createRecaptcha() {
    grecaptcha.render("captcha", {sitekey: "<?php echo $this->config->item('recaptcha_sitekey') ?>", theme: "light"});
    }
</script>

添加此代码在您想要 recaptcha 出现的位置

<div id="captcha"></div>

这就是我能做的 ^_^

演示: https://www.waraywarayako.ph/

I have my on idea how recaptcha works on Codigniter. I cannot explain it to you clearly but I hope you can understand this codes below:

Add this to your config files

#google reCaptcha
$config['recaptcha_sitekey'] = 'put site key'; //Recaptcha Site key
$config['recaptcha_secretkey'] = 'put secret key'; //Recaptcha Secret Key

Add this code to your validation form

$this->form_validation->set_rules('g-recaptcha-response', 'recaptcha validation', 'required|callback_validate_captcha');
        $this->form_validation->set_message('validate_captcha', 'Please check the the captcha form');

and dont forget to add this to the controller where the recaptcha will validate

//ADDITIONAL CODES
    function validate_captcha() {
        $captcha = $this->input->post('g-recaptcha-response');
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$this->config->item('recaptcha_sitekey')."&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);

        if ($response . 'success' == false) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

Add this codes to the form where recaptcha will appear

 <div class="g-recaptcha" data-sitekey="<?php echo $this->config->item('recaptcha_sitekey') ?>"></div> 

And don`t forget the Javascript

<script src="https://www.google.com/recaptcha/api.js"></script>

And if you want to appear recaptcha twice from main page to modal add this code to your modal

<script>
    createRecaptcha();
    function createRecaptcha() {
    grecaptcha.render("captcha", {sitekey: "<?php echo $this->config->item('recaptcha_sitekey') ?>", theme: "light"});
    }
</script>

add this code for where you want recaptcha will appear

<div id="captcha"></div>

This is all I can do ^_^

Demo: https://www.waraywarayako.ph/

夏末 2024-11-07 02:54:13

这篇 wiki 文章和库非常简单。因为您不需要的只是一个链接,这里有一些来自 wiki 文章的示例代码可以帮助您入门。

该代码确实非常不言自明。使用公钥和私钥设置您的配置,然后在控制器中加载验证码和密码。 form_validation 库并根据验证状态显示正确的视图。

配置/recaptcha:

$config['recaptcha'] = array(
  'public'=>'YOUR PUBLIC KEY',
  'private'=>'YOUR PRIVATE KEY',
  'RECAPTCHA_API_SERVER' =>'http://www.google.com/recaptcha/api',
  'RECAPTCHA_API_SECURE_SERVER'=>'https://www.google.com/recaptcha/api',
  'RECAPTCHA_VERIFY_SERVER' =>'www.google.com',
  'RECAPTCHA_SIGNUP_URL' => 'https://www.google.com/recaptcha/admin/create',
  'theme' => 'white'
);

控制器:

function index()
  {
    $this->load->library('recaptcha');
    $this->load->library('form_validation');
    $this->lang->load('recaptcha');
    $this->load->helper('form');

    if ($this->form_validation->run()) 
    {
      $this->load->view('recaptcha_demo',array('recaptcha'=>'Yay! You got it right!'));
    }
    else
    {
      //the desired language code string can be passed to the get_html() method
      //"en" is the default if you don't pass the parameter
      //valid codes can be found here:http://code.google.com/apis/recaptcha/docs/customization.html#i18n
      $this->load->view('recaptcha_demo',array('recaptcha'=>$this->recaptcha->get_html()));
    }
  } 

This wiki article and library are extremely simple. Because you don't want just a link here is some sample code from the wiki article to get you started.

The code is really quite self explanatory. Set up your config with your public and private keys, then in your controller load the recaptcha & form_validation libraries and show the proper view depending on the status of the validation.

config/recaptcha:

$config['recaptcha'] = array(
  'public'=>'YOUR PUBLIC KEY',
  'private'=>'YOUR PRIVATE KEY',
  'RECAPTCHA_API_SERVER' =>'http://www.google.com/recaptcha/api',
  'RECAPTCHA_API_SECURE_SERVER'=>'https://www.google.com/recaptcha/api',
  'RECAPTCHA_VERIFY_SERVER' =>'www.google.com',
  'RECAPTCHA_SIGNUP_URL' => 'https://www.google.com/recaptcha/admin/create',
  'theme' => 'white'
);

controller:

function index()
  {
    $this->load->library('recaptcha');
    $this->load->library('form_validation');
    $this->lang->load('recaptcha');
    $this->load->helper('form');

    if ($this->form_validation->run()) 
    {
      $this->load->view('recaptcha_demo',array('recaptcha'=>'Yay! You got it right!'));
    }
    else
    {
      //the desired language code string can be passed to the get_html() method
      //"en" is the default if you don't pass the parameter
      //valid codes can be found here:http://code.google.com/apis/recaptcha/docs/customization.html#i18n
      $this->load->view('recaptcha_demo',array('recaptcha'=>$this->recaptcha->get_html()));
    }
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文