Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Help using php no captcha recaptcha code in Smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
mcemrn
Smarty n00b


Joined: 22 Dec 2016
Posts: 3

PostPosted: Thu Dec 22, 2016 11:44 am    Post subject: Help using php no captcha recaptcha code in Smarty Reply with quote

Hello, i am following this tutorial: https://webdesign.tutsplus.com/tutorials/how-to-integrate-no-captcha-recaptcha-in-your-website--cms-23024

I have no problems with the first steps.

This does require the add of the following php code:
Code:
<?php

// grab recaptcha library
require_once "recaptchalib.php";

// your secret key
$secret = "0000000000000000000000000000000000";

// empty response
$response = null;

// check our secret key
$reCaptcha = new ReCaptcha($secret);

// if submitted check response
if ($_POST["g-recaptcha-response"]) {
    $response = $reCaptcha->verifyResponse(
        $_SERVER["REMOTE_ADDR"],
        $_POST["g-recaptcha-response"]
    );
}

?>



And also this:
Code:
    <?php
      if ($response != null && $response->success) {
        echo "Hi " . $_POST["name"] . " (" . $_POST["email"] . "), thanks for submitting the form!";
      } else {
    ?>

    <?php } ?>


I have two files: register.php and register.tpl

If i past these code into register.tpl Smarty give me fatal errors, also if i use {php}{/php} tags.
I am a beginner, can you please help me ?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Dec 22, 2016 8:56 pm    Post subject: Reply with quote

Again, please, from the beginning and slowly.
What are you doing and what Smarty gives in return?
Back to top
View user's profile Send private message
mcemrn
Smarty n00b


Joined: 22 Dec 2016
Posts: 3

PostPosted: Sat Dec 24, 2016 9:47 am    Post subject: Reply with quote

AnrDaemon wrote:
Again, please, from the beginning and slowly.
What are you doing and what Smarty gives in return?


Hello and thanks for reply.
I have a custom website, no CMS, and this website uses Smarty. The website allow users to register and uses an old image captcha system.
To disallow bots registrations, i will implement new Google No Captcha Recaptcha plugin, ( more info here: https://www.google.com/recaptcha/ ).

So i decided to follow this tutorial: https://webdesign.tutsplus.com/tutorials/how-to-integrate-no-captcha-recaptcha-in-your-website--cms-23024

I have no problem in first steps: register on Google and obtain Site Key and Secret Key.

Also no problem in following steps: insert JavaScript API and div box in register.tpl smarty template file .

My problem is in the final steps, send to Google response so it can verify it.
This was no problem in normal website to put php code in register.php normal php page.
But on Smarty Template, so in register.tpl, it gives me fatal errors, also if i use {php}{/php} special tags

Code:
<?php

// grab recaptcha library
require_once "recaptchalib.php";

// your secret key
$secret = "0000000000000000000000000000000000";

// empty response
$response = null;

// check our secret key
$reCaptcha = new ReCaptcha($secret);

// if submitted check response
if ($_POST["g-recaptcha-response"]) {
    $response = $reCaptcha->verifyResponse(
        $_SERVER["REMOTE_ADDR"],
        $_POST["g-recaptcha-response"]
    );
}

?>


And this:

Code:
 <?php
      if ($response != null && $response->success) {
        echo "Hi " . $_POST["name"] . " (" . $_POST["email"] . "), thanks for submitting the form!";
      } else {
    ?>

    <?php } ?>


I think the solution is to put this code in register.php page and import it in register.tpl page, but i am a beginner, can you please transform this code for me ?
I am sure this code will be useful for many users around the web.

EDIT: I found this old 2011 topic http://www.smarty.net/forums/viewtopic.php?p=75007 , but the code refers to old Google captcha plugin, not the new No Captcha Recaptcha, can you please update the code ?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Dec 24, 2016 6:38 pm    Post subject: Reply with quote

What is your Smarty question?
So far, you didn't show any Smarty code or anything even remotely related to Smarty.

Processing user input is entirely up to you, I'm failing to see, how it is in any way related to templates.
Back to top
View user's profile Send private message
mcemrn
Smarty n00b


Joined: 22 Dec 2016
Posts: 3

PostPosted: Sun Dec 25, 2016 9:41 am    Post subject: Reply with quote

AnrDaemon wrote:
What is your Smarty question?
So far, you didn't show any Smarty code or anything even remotely related to Smarty.

Processing user input is entirely up to you, I'm failing to see, how it is in any way related to templates.


Hello and Merry Christmas !
Please, help me, this is a Smarty related question.
In this old 2011 topic http://www.smarty.net/forums/viewtopic.php?p=75007 , User "androidworkz" solved the question with this code:

register.php
Code:
$smarty->assign("recaptcha", recaptcha_get_html($pub_key, $error));


register.tpl
Code:
{nocache}
{$recaptcha}
{/nocache}


This was for Google reCaptcha version 1.0, can you please update this code for Google reCaptcha 2.0 ? (Google No Captcha Recaptcha)

You can find the new code in my first post
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Dec 25, 2016 5:00 pm    Post subject: Reply with quote

Which "this problem" he solved?
All I see is printing the captcha to the user.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Tue Dec 27, 2016 12:10 am    Post subject: Reply with quote

In the first codeblock, you have a $response object.

In the second codeblock, you are outputting something if the $response object's success property is true.

That something can be assigned to a string. Let's also do one for failure:
Code:

$success = "Hi " . $_POST["name"] . " (" . $_POST["email"] . "), thanks for submitting the form!";
$failure = "Sorry " . $_POST["name"] . " (" . $_POST["email"] . "), try again!";

Now, there will be more code to instantiate the Smarty class. I will let you figure that out.

Next, we assign the appropriate string to a template variable:
Code:

      if ($response != null && $response->success) {
        $smarty->assign("response",$success);
      } else {
        $smarty->assign("response",$failure);
      }

Finally, we have Smarty render and display the template:
Code:

$smarty->display('register.tpl');
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Dec 27, 2016 12:49 am    Post subject: Reply with quote

Errr, now, you have to try it again, for me... please.
I didn't understand anything you've said.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP