weixin_33743703 2015-11-03 21:48 采纳率: 0%
浏览 22

添加验证码以形成php / mysql

I want to add Google captcha to my php form. The form adds data to my mysql database. How can I add the two parts of code together so the form checks first the captcha and after it's checked, then send it.

$servername = "";
$username = "";
$password = "";
$database = "";


$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 


    $email = $_SESSION['userName'];
    $contact = $_POST['naar'];
    $address = $_POST['bericht'];


$sql = "INSERT INTO messages (to_user, from_user, message)
        VALUES ('".$contact."', '".$email."', '".$address."')";


$conn->close();






  if($_SERVER["REQUEST_METHOD"] === "POST")
   {
    //form submitted

    //check if other form details are correct

    //verify captcha
    $recaptcha_secret = "xxxxxxxxxxxxxx";
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
    $response = json_decode($response, true);
    if($response["success"] === true)
    {
        echo "Logged In Successfully";
    }
    else
    {
        echo "You are a robot";
    }
}
  • 写回答

1条回答 默认 最新

  • weixin_33709609 2015-11-03 23:40
    关注

    As @Dagon and @Marc B have suggested in the comments above, try this:

    $servername = "";
    $username = "";
    $password = "";
    $database = "";
    
      if($_SERVER["REQUEST_METHOD"] === "POST")
       {
        //form submitted
    
        //check if other form details are correct
    
        //verify captcha
        $recaptcha_secret = "xxxxxxxxxxxxxx";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
        $response = json_decode($response, true);
        if($response["success"] === true)
        {
    
            //$conn = new mysqli($servername, $username, $password, $database);
            try{
                   $db = new PDO('mysql:host='.$servername.';dbname='.$database,$username,$password);
                   $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch(PDOException $e){
                  echo "Error connecting to DB";
                  echo $e->getMessage();
                  exit();
            }
    
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 
    
            $email = $_SESSION['userName'];
            $contact = $_POST['naar'];
            $address = $_POST['bericht'];
    
    
            $sql_pdo = "INSERT INTO messages (to_user, from_user, message)
                    VALUES (:contact, :email, :address)";
    
            $stmt = $conn->prepare($sql_pdo);
    
            try {
                 $result = $stmt->execute( array(
                    ':contact' => $contact,
                    ':email' => $email,
                    ':address' => $address
                    ));
                 if ( count($result) > 0 ) {
                    // Insert has gone well. Do your things here.
                    echo "Logged In Successfully";
                 }
                 else {
                     // Insert error. Report, check, ...
                 }
            }
            catch(PDOException $e){
                echo 'could not insert in DB';
                echo 'Error: ' . $e->getMessage();
                return false;
            }
    
            $conn->close();
        }
        else
        {
            echo "You are a robot";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog