(PHP) Stuck making submission form anti spam

天大地大妈咪最大 提交于 2020-01-22 02:31:15

问题


So I am almost done creating this anti spam google recaptcha v3 form. I get failure to the end. could you see what is the problem? I dont use this to send data to DB but to my email. let me know if this is the problem, but i think not. I am almost finished, here what I have done so far: still getting failed to save data error


<?php


if(isset($_POST['submit'])){
//   print_r($_POST);
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = [  
'secret' => "censord",
'response' => $_POST['token'],
'remoteip' => $_SERVER['REMOTE_ADDR']
];
$options = array(
  'http' => array(
     'header'  => "Content-type: application/
     x-www-form-urlencoded\r\n",
     'method' => 'POST',
     'content' => http_build_query($data)

  )

);

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);


$res = json_decode($response, true);
if($res['success'] == true) {
    //save data to database
    echo '<div class="alert alert-success">
    <strong>Success!</strong> Data is saved.
    </div>';
} else {
    echo '<div class="alert alert-warning">
    <strong>warning!</strong> Failed to save data.
    </div>';
}

}
?>

---------------------and the form

<?php
  if(isset($_POST['submit'])){
    $name = htmlspecialchars(stripslashes(trim($_POST['username'])));
    $email = htmlspecialchars(stripslashes(trim($_POST['email'])));
    $text = htmlspecialchars(stripslashes(trim($_POST['textt'])));
    if(!preg_match("/^[A-Za-z .'-]+$/", $name)){
      $name_error = 'Invalid name';
    }

    if(!preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/", $email)){
      $email_error = 'Invalid email';
    }
    if(strlen($text) === 0){
      $message_error = 'Your message should not be empty';
    }
    if(preg_match('/http|www/i',$comments)) {
    $error_message .= "We do not allow a url in the comment.<br />";
  }
  }
?>

  <div id="container">
    <div class="form-wrap">
         <h1>Report bug</h1>
        <p>It's free and you help us make the programming wikipedia better!</p>

        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

            <div class="form-group">

                <label for="username">Name:</label>
                <input type="text" name="username">
   <p><?php if(isset($name_error)) echo $name_error; ?></p>

            </div>

            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" name="email">
                  <p><?php if(isset($email_error)) echo $email_error; ?></p>

            </div>

            <div class="form-group">
                <label for="textx">Message:</label>
                <textarea type="text" name="textt" rows="22" cols="42"></textarea>
   <p><?php if(isset($message_error)) echo $message_error; ?></p>



                <button type="submit" name="submit" value="Submit" class="btn">Report us</button>

 <?php 
    if(isset($_POST['submit']) && !isset($name_error) && !isset($subject_error) && !isset($email_error) && !isset($message_error)){
      $to = 'censord'; // edit here
      $body = " Name: $name\n E-mail: $email\n Message:\n $message";
      if(mail($to, $subject, $body)){
        echo '<p style="color: green">Message sent</p>';
      }else{
        echo '<p>Error occurred, please try again later</p>';
      }
    }
  ?>
  <input type="hidden" id="token" name="token">
        </form>
      </div>

    </div>

  </body>
  <script>
grecaptcha.ready(function() {
    grecaptcha.execute('censord', {action: 'homepage'}).then(function(token) {
       console.log(token);
       document.getElementById("token").value = token;
    });
});
</script>
</html>

来源:https://stackoverflow.com/questions/59561560/php-stuck-making-submission-form-anti-spam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!