Yes, that’s it! Now, I can’t comment on why that’s not appearing in the $_POST array but that’s the data you’re after. Do the other form fields appear in the $_POST array or is $_POST completely empty, like you mentioned above? If you can, try looking at everything in the $_POST array to see what you’re getting from the form. Maybe the value you need is there but not located where you expect it to be. Your original code above looked correct, so I’m not sure why the response token isn’t being returned where you expect it to be. Now you know you’re actually getting the token you need, you’re getting close!
Cool. So, what happens with $_POST if the Turnstile div isn’t in the form at all? I mean if you’re not getting ANY form data, that would indicate something else is wrong. Unless you’re saying $_POST is empty only when the Turnstile div is in the form.
If you want to block the actual submit, I think you’ll need to do that on the client side via JavaScript. You can check to see if the response token was returned from Cloudflare and if it wasn’t block the form from being sent to the server. You can see some discussion about that here:
that thread is about using an invisible Turnstile widget, but the discussion about interrupting the form submission process still applies, in my opinion.
What you can do is create a small JavaScript function that’s “bound” to the “onSubmit” event on the form. Have that function return “true” to allow submission of the form and “false” to prevent submission.
Try fetching the response when the form is submitted. I haven’t used the “onload” event on loading the Turnstile script but does that fire after the script has been loaded vs after the widget has been rendered? If you want to capture the response token before the form is submitted, you’ll need to grab it after the widget has been rendered. I still think you’ll need to do this on the browser side which will mean JavaScript. Let me try something and I’ll post a code snipet you can experiment with.
I’m half-way through switching to explicit rendering from implicit thinking that might do something for me :-/
I’ll clean house, and stare at your snippet over a bowl of ramen.
Then see what I can manage!
Cool! So, one idea is this: put your own, hidden form field in your form to hold the response. Use JavaScript to capture the response sent from Cloudflare and save it in your form field. Then, see if that field is in the $_POST array when you submit the form.
(1) render the client side widget
(2) it shows ‘Success’
(3) click Submit
(4) extract token
(5) serververify token with CF API
(6) if token OK, do stuff
I understand the need for (5)
I’m unclear why (2) is ever be displayed to a user as ‘Success’ if Submit-then(5) hasnt’ happened yet, and MIGHT return not-OK.
Great! Thanks for the update! I don’t know the internals of Turnstile but I think (assume) “Success” means the browser challenges were successful. So, as far as Turnstile is concerned, you’re a human. The need to verify the token is to protect against that token being forged by someone trying to abuse your form.
In any event, glad you were able to locate your form data!
I’ve seen “every OTHER post on the 'net”, except that one.
And adding the name= attr fixes it.
Value’s now in $_POST, as you’d expect.
Thx!
Good news is it’s starting to
Still trying to wrap head around the widget re-rendering – I assume with a 2nd API check – on server-verify.
Could possibly stop that with ajax, or ESI, but really not sure I care.
Groovy! Glad was of use to you! So, are things pretty much working now? Were you able to get your original code to work, as posted above? I mean not counting the form field change.
I still can’t convince myself that I correctly wrapped the submit button conditionally.
I want to make sure that [on,after}-Submit actions only continue IF the CF server-verify comes back OK.
Other than that it seems to be working.
I’m working on removing useless code and cleaning up a bit.
But it’s starting to look like an easy-to-drop-in CF-preotected custom-login page with no plugin dependencies.