如何为我自己的联系表添加自己的验证码

how to add own CAPTCHA for my own contact form

我正在使用 Dreamweaver cc 创建一个表单。该表格是用 php 编写的并且运行良好。现在我想为验证方法添加验证码图像或文本。如何使用我的代码添加验证码。

我的表单看起来像这样

<table width="100%" border="1" align="center" cellpadding="2">
<form method="post" action="contact_process.php"><tr>
<td><input type="text" name="name" placeholder="Enter Name" class="add_input_data" required/></td>
</tr>
 <tr>
 <td><input type="text" name="mobile" placeholder="Enter Mobile" class="add_input_data" required/></td>
</tr>
<tr>
 <td><select name="location" class="add_input_data">
 <option selected disabled>Location</option>
<option value="chittore">Chittore</option>
<option value="koduru">Koduru</option>
<option value="other">Other</option>
</select></td>
</tr>
<tr>
 <td><textarea name="address" class="add_input_textarea" placeholder="Enter Address" required></textarea></td>
</tr>
<tr>
 <td><input type="submit" name="submit" value="Submit" class="add_input_submit" align="right"/></td>
</tr></form>
</table>

我的表单处理脚本是这样的

<?php 

$name = $_POST['name'];  
$mobile = $_POST['mobile'];  
$location = $_POST['location']; 
$address = $_POST['address']; 



$formcontent="From: $name \n Mobile : $mobile \n Location: $location \n      Address : $address";  
 $recipient = "dovariramu@gmail.com";  
  $subject = "New Connection Query";  


  mail($recipient, $subject, $formcontent) or die("Error!");  

 header('Location: thankyou.php');

 ?>

和我的 thankyou.php 页面有一些内容。这里我就不展示了

现在我有两个问题

1) 当用户填写数据并单击提交按钮时,表单显示来自 thankyou.php 的成功完整消息。但我想在页面中显示成功的完整消息(带有简单的滚动成功完整消息)。 2) 我想为这个表格添加一个验证码。如何做到这一点?

对于您使用的验证码,rand() 方法如下:

<?php 
  $a = rand(1,9);
  $b = rand(1,9);
  $c = $a + $b; 
?>

在html中写入

<h5> What is <?php echo $a; ?> + <?php echo $b; ?> ? </h5>
<input type="text" name="txt_value" id="txt_value" />

现在您可以匹配 jquery 中的两个值 ( $c with $("#txt_value").val() ) 或在提交表单时匹配,只要您觉得方便即可。

希望对您有所帮助...

验证码教程HERE。这解决了我的问题。

为了简化此操作,请使用以下 php 代码

 function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
    $n = rand(0, $alphaLength);
    $pass[] = $alphabet[$n];
      }
    return implode($pass); //turn the array into a string
    }


echo $pwd=randomPassword();

添加验证码的最简单方法是使用 google 的新 reCaptcha. Its simple to use you just need to register at https://www.google.com/recaptcha/ and get a site key and a secret code. I am using this on my website and it works well. After registration you will have to add a simple div element where you want the CAPTCHA to render.and a simple code to implement it that you can find here

ITS 100% WORK TRY It, copy div tag & past in html 然后按照代码......

    <div style="padding: 1%" align="left">
    <img src="captcha.php" height="20" width="81" />&nbsp;&nbsp;<input style="height: 35px;width:235px;border-radius: 5px" type="number" name="CODE" ></br></br></div>


           if(isset($_POST['LOGIN'])) {

             if(empty($_POST['EMAIL']) || empty($_POST['PASSWORD'])||empty($_POST['CODE']))
                  {echo "<script>alert('Insert all Filds'); location ='login.php';</script>";

        }
        else
        {
                session_start();
                $EMAIL = $_POST['EMAIL'];
                $PASSWORD = $_POST['PASSWORD']; //echo $EMAIL . $PASSWORD;die();
                $CAPTCHA = $_POST['CODE'];
                $C = $_SESSION['x']; //SESSION ['X'] is define in captcha file so we compaire it with captcha below

                if($CAPTCHA != $C) {
                    echo "<script>dialog('Invelid Captcha Code'); location ='login.php';</script>";session_destroy();}

                else{
                $sql = "SELECT * FROM register WHERE EMAIL = '$EMAIL' AND PASSWORD = '$PASSWORD' "; 
                $result = mysqli_query($conn,$sql);}

captcha.php 文件

    session_start();
    header('content-type:image/jpeg');
    $mj = $_SESSION['x'] = mt_rand(1111,9999);
    $image = imagecreate(100, 50);

    imagecolorallocate($image, 215, 215, 215);
    $txtcolor = imagecolorallocate($image, 0, 0, 0);
    imagettftext($image,20,0,20,40,$txtcolor,'font.ttf',$mj);

    imagejpeg($image);