所以我最近不得不在我的公司网站上添加一个recaptcha,我过去在其他网站上实现了这个,所以我想我会使用该代码并替换网站密钥,它会起作用。到目前为止还没有。
我的公司网站查看:
<?php echo $this->Form->create(null, ['url' => ['controller' => 'Content', 'action' => 'qbsignup']]); ?>
<?php echo $this->Form->input('full_name',['type'=>'text', 'required'=>true]); ?>
<?php echo $this->Form->input('email',['type'=>'email', 'required'=>true]); ?>
<?php echo $this->Form->input('phone_number',['type'=>'text', 'required'=>false]); ?>
<div class="g-recaptcha" data-sitekey="mysitekey"></div>
<?php echo $this->Form->submit('Sign Up'); ?>
<small>By submitting this form, you agree to allow Net2Sky to contact you about QuickBooks related information.</small>
<?php echo $this->Form->end(); ?>
虽然这是我公司的控制人员:
if(!empty($this->request->data)) {
//verify google recaptcha
$url = "https://www.google.com/recaptcha/api/siteverify";
$privatekey='myprivatekey';
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".($this->data['g-recaptcha-response'])."&remoteip=".$_SERVER['REMOTE_ADDR']);
//$url."?secret".$privatekey."&response=".($this->data['g-recaptcha-response'])."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data2 =json_decode($response, true);
if(intval($data2["success"])==1){
$error=0;
}else{
$error=1;
$this->Session->setFlash("Please fill out the Security CAPTCHA and try again.");
}
if ($error == 0) {
foreach($this->request->data as $n => $m):
$body .= "<b>" . $n . ":</b> " . $m . "<br />";
endforeach;
$body .= "</p><p>Date Of Submission: " . date("m/d/Y H:i") . "<p>";
$email = new Email('default');
$result = $email ->from(['aemailaddress' => 'Our Site'])
->to('aemailaddress')
->emailFormat('both')
->subject('QuickBooks Signup')
->send($body);
if($result) {
$this->redirect('/qbsignup/'.$this->request->data['full_name']);
}
else {
$this->redirect('/qbsignup');
}
}
else {
$this->set('name', $fullname);
}
}
所以我有点困惑,为什么它不起作用,只返回失败的代码。
以下是代码工作的其他网站的信息: 视图:
<?php echo $this->Form->create(['url' => '/Forms/contact']); ?>
<?php echo $this->Form->input('Contact.name', array('label' => 'Name', 'size' => '33', 'required' => 'required')); ?>
<?php echo $this->Form->input('Contact.phone', array('label' => 'Phone Number', 'size' => '20', 'required' => 'required')); ?>
<?php echo $this->Form->input('Contact.email', array('label' => 'E-mail Address', 'size' => '22', 'required' => 'required')); ?>
<?php echo $this->Form->input('Contact.topic', array('label' => 'What service are you interested in?', 'options'=>
array('Tree Removal'=>'Tree Removal','Tree Trimming'=>'Tree Trimming','Mulch Delivery'=>'Mulch Delivery','Emergency Service'=>'Emergency Service','Other'=>'Other'))); ?>
<?php echo $this->Form->input('Contact.needs', array('label' => 'How may we help you?', 'after' => '<p style="padding: 0;margin: 0; font-size: 80%;">(we\'ll respond within 1 hour or less between 7 am and 7 pm)</p>', 'rows' => '5', 'cols' => '45', 'required' => 'required')); ?>
<?php echo $this->Form->hidden('Contact.page', array('value' => 'home')); ?>
<div class="g-recaptcha" data-sitekey="sitekey"></div>
<?php echo $this->Form->submit(); ?>
<?php echo $this->Form->end(); ?>
它是控制器:
if (!empty($this->data['Contact'])) {
//verify google recaptcha
$url = "https://www.google.com/recaptcha/api/siteverify";
$privatekey='myprivatekey';
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".($this->data['g-recaptcha-response'])."&remoteip=".$_SERVER['REMOTE_ADDR']);
//$url."?secret".$privatekey."&response=".($this->data['g-recaptcha-response'])."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data2 =json_decode($response, true);
if(intval($data2["success"])==1){
$error=0;
}else{
$error=1;
$this->Session->setFlash("Please fill out the Security CAPTCHA and try again.");
}
if ($error == 0) {
// Craft e-mail
$subject = "Website Contact";
$message = "";
foreach ($this->data['Contact'] as $key => $value) {
$message .= '{' . $key . '} = > ' . $value . "\n";
}
$message .= '{timestamp} => ' . date('F j, Y \a\t g:i a', time()) . "\n";
$message .= '{sent from IP} => ' . substr($this->request->clientIp(), 0, 12);
// $this->Email->to = array('aemailaddress');
// $this->Email->from = $this->data['Contact']['email'];
// $this->Email->template = 'default';
// $this->Email->subject = $subject;
if(substr($this->request->clientIp(), 0, 12) != "aipaddress" &&
substr($this->request->clientIp(), 0, 11) != "aipaddress"){
$message .= '{timestamp} => ' . date('F j, Y \a\t g:i a', time()) . "\n";
$message .= '{sent from IP} => ' . substr($this->request->clientIp(), 0, 12);
$Email = new CakeEmail();
$Email ->from(array('aemailaddress' => 'Website Submission'))
->to(array('aemailaddress'))
->subject($subject)
->sender('aemailaddress', 'Website Submission')
->config(array('from' => 'aemailaddress', 'transport'=>'Smtp'))
->send($message);
//$this->Email->send($message);
// print_r($this->Email);
// exit();
$this->Session->setFlash("Thank you for your submission");
}
else
{
$this->Session->setFlash("Nice try, spammer.");
}
$this->redirect("/");
}
}
如果有人有任何想法,为什么一个有效,另一个没有,那将是非常有帮助的。或者,如果有人知道为什么一个不起作用并且可以帮助修复。