how to add own CAPTCHA for my own contact form

折月煮酒 提交于 2020-03-16 07:51:13

问题


I am creating a form using dreamweaver cc. the form is written in php and working good. now i want to add captcha image or text for verification method. how to possible using my code to add a captcha.

My form looking like this

<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>

my form processing script is looking like this

<?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');

 ?>

and my thankyou.php page with some content. here i am not showing.

Now I have two questions

1) when a user fill the data and click the submit button, the form display success full message from thankyou.php. but i want to display success full message with in a page(with simple scrolling success full message). 2) I want to add a captcha for this form. How to do this ?


回答1:


for captcha you used, rand() method like:

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

In html, write

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

And now you can match both values ( $c with $("#txt_value").val() ) in jquery or on form submission, whatever make you easy.

Hope it helps...




回答2:


The simplest way to add a CAPTCHA is to use google's new 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.




回答3:


captcha code tutorial HERE.This solve my problem .

For make this simple use the following php code

 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();



回答4:


ITS 100% WORK TRY It, copy div tag & past in html and do follow the code......

    <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 file

    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);


来源:https://stackoverflow.com/questions/29115270/how-to-add-own-captcha-for-my-own-contact-form

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