如何为我自己的联系表单添加自己的CAPTCHA

时间:2015-03-18 06:12:09

标签: php forms dreamweaver

我正在使用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)我想为此表单添加验证码。怎么做?

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您使用的验证码,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() ),无论您做什么都可以。

希望有所帮助......

答案 2 :(得分:0)

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

答案 3 :(得分:0)

ITS 100%可以尝试,复制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);