我目前正在使用运行Apache和PHP 5.6的网络服务器从头创建一个个人网站。我制作了框架,一些页面和一些CSS。我目前在将Google的reCaptcha v2集成到我的联系表单时遇到了问题。我能够通过HTML将它集成到表单中,我的AJAX脚本工作正常,但我不能为我的生活弄清楚如何将captcha检查正确地集成到我的PHP脚本中。我想要喜欢PHP,我将不得不更多地使用它,但尝试让它工作非常令人沮丧。
这个表单在没有catcha集成的情况下工作得很好,但是当我将这些更改引入PHP脚本时,它无法完成并且不会发送消息。我一直在追查确切的问题,但无法弄明白。我很乐意使用JS alert()来存根脚本的不同部分,但由于它是PHP我不能这样做:c我还使用了一些验证器来确保PHP的语法是正确的。我的IDE中没有错误。
任何人都可以通过以下PHP表单脚本看到任何荒谬的问题吗?
PHP
<?php
// Only process POST reqeusts.
if ( $_SERVER[ "REQUEST_METHOD" ] == "POST" ) {
// Get the form fields and remove whitespace.
$firstname = strip_tags( trim( $_POST[ "firstname" ] ) );
$firstname = str_replace( array( "\r", "\n" ), array( " ", " " ), $firstname );
$lastname = strip_tags( trim( $_POST[ "lastname" ] ) );
$lastname = str_replace( array( "\r", "\n" ), array( " ", " " ), $lastname );
$email = filter_var( trim( $_POST[ "email" ] ), FILTER_SANITIZE_EMAIL );
$phone = filter_var( trim( $_POST[ "phone" ] ), FILTER_SANITIZE_NUMBER_INT );
$message = trim( $_POST[ "message" ] );
$validation = false;
function buildCaptchaUrl() {
$captcha = $_POST[ 'g-recaptcha-response' ];
$secret = 'SECRET';
return "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=" . $captcha . "&remoteip=" . $_SERVER[ 'REMOTE_ADDR' ];
}
function fileGetContentsCurl( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$data = curl_exec( $ch );
curl_close( $ch );
return $data;
}
function sendCaptchaResponse() {
$response = json_decode( file_get_contents_curl( buildCaptchaUrl() ), true );
if ( $response[ 'success' ] == false ) {
return false;
}
return true;
}
//The problematic chain of events is caused by this
$validation = sendCaptchaResponse();
if ( $validation == false ) {
//captcha failed
http_response_code( 403 );
echo "Please verify your humanity by using the captcha.";
exit;
} else if ( $validation == true ) {
//captcha passed
// Check that data was sent to the mailer.
if ( empty( $firstname )OR empty( $message )OR!filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
// Set a 400 (bad request) response code and exit.
http_response_code( 400 );
echo "Some form fields must have been empty. Please complete the form and submit again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "chris@chillstice.com";
// Set the email subject.
$subject = "Form Submission by $firstname $lastname";
// Build the email content.
$email_content = "Name: $firstname $lastname\n";
$email_content .= "Email: $email\n";
$email_content .= "Phone: $phone\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $firstname $lastname <$email>";
// Send the email.
if ( mail( $recipient, $subject, $email_content, $email_headers ) ) {
// Set a 200 (okay) response code.
http_response_code( 200 );
echo "Your message has been sent and I will be in contact soon.";
} else {
// Set a 500 (internal server error) response code.
http_response_code( 500 );
echo "Something went wrong and we couldn't send your message.";
}
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code( 403 );
echo "There was a problem with your submission, please try again.";
}
?>
HTML
<form id="form" class="item needs-validation" method="POST" action="contact-form.php" novalidate>
<div class="form-row">
<div class="form-group col-md-6">
<label for="firstname">First name *</label>
<input id="firstname" name="firstname" class="form-control" placeholder="John" type="text" required maxlength="100">
<div class="valid-feedback">What a lovely name!</div>
<div class="invalid-feedback">That's no name.</div>
</div>
<div class="form-group col-md-6">
<label for="lastname">Last name *</label>
<input id="lastname" name="lastname" class="form-control" placeholder="Doe" type="text" required maxlength="100">
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">That's not a real name.</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="email"><i class="far fa-envelope mr-2"></i>Email *</label>
<input id="email" name="email" class="form-control" placeholder="someone@domain.com" type="email" required maxlength="100">
<div class="valid-feedback">Valid</div>
<div class="invalid-feedback">That's not a real email...</div>
</div>
<div class="form-group col-md-6">
<label for="phone"><i class="fas fa-mobile-alt mr-2"></i>Phone</label>
<input id="phone" name="phone" class="form-control" placeholder="1234567890" type="tel" maxlength="20">
<div class="valid-feedback">Not required.</div>
<div class="invalid-feedback">That's not a real phone number.</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-12">
<label for="message"><i class="fa fa-comment-alt mr-2"></i>Message *</label>
<textarea id="message" name="message" class="form-control" placeholder="" maxlength="100000"></textarea>
<div class="valid-feedback">Nice message.</div>
<div class="invalid-feedback">I have no idea what you did, but that's not valid.</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<div class="g-recaptcha" data-sitekey="6LdOqVsUAAAAAN25grBs05Ip8JmjGQNqURivfH0y"></div>
</div>
<div class="form-group col-md-6">
<button id="submit" name="submit" type="submit" class="btn btn-primary btn-lg" style="float: right;">
Submit
</button>
</div>
</div>
</form>
JS AJAX
$(function() {
"use strict";
// Get the form.
var form = $('#form');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
// Set the message text.
$('#alert-form-success-text').text(response);
$('#alert-form-success').css({
"visibility": "visible",
"opacity": "1"
});
$('#alert-form-fail').css({
"visibility": "hidden",
"opacity": "0"
});
})
.fail(function(data) {
$('#alert-form-fail').css({
"visibility": "visible",
"opacity": "1"
});
$('#alert-form-success').css({
"visibility": "hidden",
"opacity": "0"
});
// Set the message text.
if (data.responseText !== '') {
$('#alert-form-fail-text').text(data.responseText);
} else {
$('#alert-form-fail-text').text('An internal error occured and your message could not be sent.');
}
});
});
});
该问题仅涉及reCaptcha集成,脚本获取POST信息和电子邮件的能力很好。我认为这个问题已经局限于
启动的方法链$validation = sendCaptchaResponse();
如果您有任何疑问,请随时提出。 这是包含以下格式的页面:https://chillstice.com/contact
答案 0 :(得分:0)
PHP文件中的fileGetContentsCurl()函数作为'file_get_contents_curl()'运行
修复此错字并使方法以一致的名称解决此问题。我确定希望我的IDE或调试工具能帮助我修复这一(1)错误,但这对你来说就是PHP。
我要感谢Karlo Kokkak暗示我的错字。
完成更改后,整个脚本按预期工作。