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


how to add own CAPTCHA for my own contact form

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

我的表格看起来像这个

<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的successfull消息。但我希望在页面中显示successfull消息(通过简单的滚动successfullmessage)。2) 我想为这个表格加一个captcha。怎么做?

添加CAPTCHA最简单的方法是使用谷歌的新reCaptcha。使用起来很简单,您只需在https://www.google.com/recaptcha/并获得一个站点密钥和一个密码。我在我的网站上使用这个,效果很好。注册后,您必须在想要CAPTCHA渲染的地方添加一个简单的div元素,并在这里找到一个实现它的简单代码。

captcha代码教程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();

ITS 100%WORK TRY It,复制div标记&过去在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);

对于您使用的captcha,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() ),无论什么都可以。

希望它能帮助