Here is my PHP Code:
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => '',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
die('MF005');
} else {
if ($mail->send()) {
$send_arEmail = $autoresponder->Send();
}}
and here is my JavaScript code:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("i-recaptcha").submit();
}
</script>
This is my captcha:
<div class="g-recaptcha" data-sitekey="" ></div>
and this is button submit:
<button class="button rounded" type="submit" data-callback="onSubmit">Send</button>
This is also my form id:
<form id="i-recaptcha">
This above function is working, but after submit form, still captcha is checked, I want to reset it to unchecked once user click on submit button.
You can use it like this. It's based on version.
Version 3
grecaptcha.ready(function() {
grecaptcha.execute('SITE_KEY', {action: 'homepage'}).then(function(token){
$('#token').val(token);
});
});
Version 2
grecaptcha.reset();
Version 1
Recaptcha.reload();
Related
My reCaptcha V2 "I'm not a robot" is failing on the server side.
It seems to be failing at:
post_captcha($_POST['g-recaptcha-response'])
Both the public & pvt key are correct and on the correct domain.
It's been 3 hours since I've created the reCAPTCHA so maybe it takes a while to process my domain through the system? I'm not sure....
Here is the HTML:
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="contact-form.php" method="POST">
<div class="g-recaptcha" data-sitekey="PUBLIC KEY"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here is the PHP:
<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => 'PVT KEY',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
} else {
echo 'success';
}
}
?>
Any help will be greatly appreciated!
Thank you.
Long time no answer. The issue was because of my web service provider having limitations on the shared server I was subscribe to e.g. not being able to edit php.ini config file.
Solution: don't use a shared server!
I have a little project in which I try to fetch data from a domain and put this information in input fields.
The Curl function is good and working. However, the jQuery script if not working or filling the input fields. If I use $url = "http://domain..."; , all is working on page load but if I use an input field with a button and post form, the fields are empty. The curl is working and gives the full page back.
How I can load the script with the same button but after load the curl script?
Button:
<form action="" method="POST">
<label for="name">URLinput</label>
<input type="url" id="inf_endpoint" name="inf_endpoint" value="" />
<button type="submit" name="mytest">Test This</button>
</form>
What I have tried:
<?php
if(isset($_POST['mytest'])){
$url=$_POST['inf_endpoint'];
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, html_entity_decode($url));
$data = curl_exec($ch);
if($result === false){
echo 'Curl error: ' . curl_error($ch);
}else{
echo 'All is good';
?>
<script>
jQuery.ajax({
url: '<?php echo site_url('admin/matches/manage'); ?>',
type: 'GET',
success: function(res) {
var data = jQuery.parseHTML(res);
jQuery(data).find('div.right').each(function(){
$('#date').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:first').each(function(){
$('#team1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:nth-child(2)').each(function(){
$('#team2').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.match_head .left a:first').each(function(){
$('#league').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.score').each(function(){
$('#result').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team_logo a:first').each(function(){
$('#logo1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.oppo2 a:first').each(function(){
$('#logo2').val(jQuery(this).html());
});
}
});
</script>
<?php
}
curl_close($ch);
echo $data;
}
?>
But this is working on Page load. But not with a if statement with click
$url="https://thedomain";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, html_entity_decode($url));
$data = curl_exec($ch);
if($result === false){
echo 'Curl error: ' . curl_error($ch);
}else{
echo 'All is good';
}
curl_close($ch);
echo $data;
and
jQuery.ajax({
url: '<?php echo site_url('admin/matches/manage'); ?>',
type: 'GET',
success: function(res) {
var data = jQuery.parseHTML(res);
jQuery(data).find('div.right').each(function(){
$('#date').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:first').each(function(){
$('#team1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:nth-child(2)').each(function(){
$('#team2').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.match_head .left a:first').each(function(){
$('#league').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.score').each(function(){
$('#result').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team_logo a:first').each(function(){
$('#logo1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.oppo2 a:first').each(function(){
$('#logo2').val(jQuery(this).html());
});
}
});
Without click function > The Page is loading and fill the fields.
cURL is a security risk even when your dealing with your servers but let me try to point out some items. Someone can get between you and your curl, magic can happen.
Your first line I don't think it's doing the proper check
<?php
if(isset($_POST['mytest'])){
change to
<?php
if(isset($_POST['Submit'])){
or even better to
if($_SERVER['REQUEST_METHOD']=='POST'){
Secondly, I can't see where $result is being set, change the following
if($result === false){
to
if(empty($data)){
I hope that solves missing points
I have a Mailchimp signup form on a client's website which was working fine until this week, I don't know what happened but when I submit a new email address it returns an error which I can't see in the debugger tool.
I don't know if it has anything to do with the code or if Mailchimp made changes to their system but here's the code used for the signup form, I haven't made this plugin so I don't know if anything is missing.
HTML
<form action="" class="c-mailchimp">
<input type="text" class="mce-validator" value="" style="display: none;">
<input type="email" class="with-button mce-EMAIL" placeholder="Votre courriel" required>
<button type="submit" class="stick-to-input mc-embedded-subscribe">
<i class="fa fa-paper-plane-o" aria-hidden="true"></i>
</button>
<div class="mce-responses">
<div class="c-mailchimp__response mce-error-response" style="display:none">Oups, une erreur est survenue..</div>
<div class="c-mailchimp__response mce-success-response" style="display:none">Merci de vous ĂȘtre inscrit !</div>
</div>
</form>
custom-mce.php
<?php
/*
Plugin Name: Agence CC
Plugin URI: http://wordpress.org/extend/plugins/agence-cc/
Version: 1
Author: Agence CC
Description:
Text Domain: agencecc
License: GPLv3
*/
add_action( 'wp_ajax_submitMailchimpNewUser', 'submitMailchimpNewUser' );
add_action( 'wp_ajax_nopriv_submitMailchimpNewUser', 'submitMailchimpNewUser' );
function submitMailchimpNewUser() {
require_once 'includes/mailchimp.php';
$newUserEmail = $_POST['email'];
$subscribeResult = syncMailchimp($apiKey, $listId, $newUserEmail);
echo $subscribeResult;
die();
}
?>
mailchimp.php
<?php
$apiKey = 'Hidden for security purpose';
$listId = 'Hidden for security purpose';
function syncMailchimp($apiKey, $listId, $email) {
$memberId = md5(strtolower($email));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/'.$memberId;
$json = json_encode([
'email_address' => $email,
'status' => "subscribed",
]);
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($json);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlInfo = curl_getinfo($ch);
curl_close($ch);
if($httpCode===200) {
return 'true';
} else {
return 'false';
}
}
Inside the app.js
var initMailChimp = function() {
var $submitButton = $('.mc-embedded-subscribe');
$submitButton.click(function(event){
event.preventDefault();
var $email = $(this).closest('form').find('.mce-EMAIL');
var email = $email.val();
var success = $(this).closest('form').find('.mce-success-response');
var error = $(this).closest('form').find('.mce-error-response');
var validator = $(this).closest('form').find('.mce-validator').val();
if(validator!=='') {
error.hide();
success.slideDown(300);
$email.val('');
} else {
$.post(
ajaxurl,
{
'action': 'submitMailchimpNewUser',
'email': email
},
function(response){
if(response==='true') {
error.hide();
success.slideDown(300);
$email.val('');
} else {
success.hide();
error.slideDown(300);
}
}
);
}
});
};
i have a json encoded data in a variable named $json, it looks like-
string(1243) "{"screenShareCode":"882919360",
"appletHtml":"",
"presenterParams":"aUsEN5gjxX/3NMrlIEGpk0=",
"viewerUrl":"http://api.screenleap.com/v2/viewer/882919360?accountid=mynet",
"origin":"API"}"
}
i need to pass this json data into javascript function, please see below
script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js">/script>
script type="text/javascript">
window.onload = function() {
var screenShareData = '?php echo $json;?>';
screenleap.startSharing('DEFAULT', screenShareData);
};
/script>
when i am trying to run this code it is giving me an error saying "missing mandatory screen share data".
How to solve this error?
i am following "https://www.screenleap.com/api/presenter"
It looks like $json is a string, you need to pass in a json object. Try the following:
window.onload = function() {
var screenShareData = '?php echo $json;?>';
screenleap.startSharing('DEFAULT', JSON.parse(screenShareData));
};
This is how you implement it based on the documentation
https://www.screenleap.com/api/presenter
<?php
// Config
$authtoken = '';
$accountid = '';
// 1. Make CURL Request
$url = 'https://api.screenleap.com/v2/screen-shares';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken:<authtoken>'));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'accountid=<accountid>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, true);
?>
<!-- 2. Launch the Presenter App -->
<script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
<script type="text/javascript">
window.onload = function() {
screenleap.startSharing('DEFAULT', JSON.parse('<?php echo $json; ?>'));
};
</script>
If this doesn't work, you got to report it to screenleap.
You should only need to actually parse the JSON if you want to access the values. Otherwise, just pass the response data right into the startSharing function, like this:
<?php
$url = 'https://api.screenleap.com/v2/screen-shares';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken:<your authtoken>'));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'accountid=<your accountid>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, true);
?>
<script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
<script type="text/javascript">
window.onload = function() {
screenleap.startSharing('DEFAULT', <?php echo $data; ?>);
};
</script>
If you just insert your own accountid and authtoken (without leading spaces), that should work for you.
i am trying to send an array of JSON objects to
PHP server using an hidden field
but all i am getting in the server is just a string
this is my javascript
function CreateArrayOfJSON()
{
var all_childrens = $('#form_div').find('*');//get all root element childrens
var form_elements = {
elements: []
};
for(var i=0;i<all_childrens.length;i++)
{
var id='#'+$(all_childrens[i]).attr('id'); //get id
var style_attr=$(all_childrens[i]).attr('style'); //get style inline
var classes=$(all_childrens[i]).attr("class");
form_elements.elements.push
({
"id" : id,
"style_attr" :style_attr,
"classes" :classes
});
}
document.getElementById('form_elements_array').value=form_elements;//fill hidden field
}
this is my PHP:
this will return Object object (as in the picture above)
$form_elements=$_POST['form_elements_array'];
this will return null
$form_elements=json_decode($_POST['form_elements_array']);
any ideas ?
thank you
document.getElementById('form_elements_array').value=JSON.stringify(form_elements);
can you paste the output string ?
or alternatively
You can use Curl for the same
$url = "URL Where you want to send data";
$ch = curl_init();
$attachment = array(
'name' => 'Your first field',
'link' => 'Second Field',
'description' => 'description here',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result = curl_exec($ch);
curl_close($ch);
now php code on the receiver page
can you paste the output string ?
or alternatively
You can use Curl for the same
$url = "URL Where you want to send data";
$ch = curl_init();
$attachment = array(
'name' => 'Your first field',
'link' => 'Second Field',
'description' => 'description here',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result = curl_exec($ch);
curl_close($ch);
now php code on the receiver page
echo $_POST['name']; // get your data with POST method