简单的PHP联系人表单+jscript验证+anti-bot

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的PHP联系人表单+jscript验证+anti-bot相关的知识,希望对你有一定的参考价值。

A simple to use framework for a contact form. This is missing some serious validation and such but is a good startin' point for any simple contact form.

The input field named 'botty' is an input field we hide with css.. if data is sent to the handling file from that field then we know a non-human filled this form out.. and it kills the script.

** just added some simple jscript to handle some mild validation.
  1. CSS
  2. <style type="text/css"> #botty {display:none;} <style>
  3.  
  4. <script type="text/javascript">
  5.  
  6. function checkform(form){
  7.  
  8. // check first name
  9. if (form.FirstName.value == "") {
  10. alert( "Please enter your First Name." );
  11. form.FirstName.focus();
  12. return false ;
  13. }
  14.  
  15. // check last name
  16. if (form.LastName.value == "") {
  17. alert( "Please enter your Last Name." );
  18. form.LastName.focus();
  19. return false ;
  20. }
  21.  
  22. // check email field
  23. if (form.email.value == "") {
  24. alert( "Please enter your Email Address." );
  25. form.email.focus();
  26. return false ;
  27. }
  28.  
  29. // check for valid email addy
  30. var apos=form.email.value.indexOf("@");
  31. var dotpos=form.email.value.lastIndexOf(".");
  32.  
  33. if (apos<1||dotpos-apos<2){
  34. alert("Not A Valid Email Address!");
  35. return false;
  36. }
  37.  
  38. // ** END **
  39. return true ;
  40.  
  41. }
  42. </script>
  43.  
  44.  
  45.  
  46. <form action="contact_handle.php" method="post" name="formy" id="formy">
  47. <p>* Required</p>
  48. <table cellspacing="0" cellpadding="2" border="0" class="webform">
  49. <tbody>
  50. <tr>
  51. <td><label for="Title">Title</label>
  52. <br />
  53. <select name="Title" id="Title" class="cat_dropdown_smaller">
  54. <option value="126631">DR</option>
  55. <option value="126629">MISS</option>
  56. <option value="126627" selected="selected">MR</option>
  57. <option value="126628">MRS</option>
  58. <option value="126630">MS</option>
  59. </select></td>
  60. </tr>
  61. <tr>
  62. <td><label for="FirstName">First Name</label>
  63. <br />
  64. <input name="FirstName" type="text" class="cat_textbox" id="FirstName" size="35" maxlength="255" />
  65. *</td>
  66. </tr>
  67. <tr>
  68. <td><label for="LastName">Last Name</label>
  69. <br />
  70. <input name="LastName" type="text" class="cat_textbox" id="LastName" size="35" maxlength="255" />
  71. *</td>
  72. </tr>
  73. <tr>
  74. <td><label for="email">Email Address</label>
  75. <br />
  76. <input name="email" type="text" class="cat_textbox" id="email" size="35" maxlength="255" />
  77. *</td>
  78. </tr>
  79. <tr>
  80. <td><label for="comments">Comments/Enquiries</label>
  81. <br />
  82. <textarea name="comments" cols="45" rows="8" class="cat_listbox" id="comments" onkeydown="if(this.value.length&gt;=1024)this.value=this.value.substring(0,1023);"></textarea></td>
  83. </tr>
  84. <tr>
  85. <td><input name="subscribe" type="checkbox" id="subscribe" value="yes" />
  86. Subscribe to: Email List Signup</td>
  87. </tr>
  88. <tr>
  89. <td><input type="submit" class="cat_button" value="Submit" id="catwebformbutton" />
  90. <span id="formHide"><input type="text" id="botty" name="botty" /></span>
  91. </td>
  92. </tr>
  93. </tbody>
  94. </table>
  95.  
  96. </form>
  97.  
  98.  
  99.  
  100. PHP
  101.  
  102. <?php
  103.  
  104. //form data
  105. $FirstName = strip_tags($_POST['FirstName']);
  106. $LastName = strip_tags($_POST['LastName']);
  107. $email = strip_tags($_POST['email']);
  108. $subscribe = strip_tags($_POST['subscribe']);
  109. $comments = strip_tags($_POST['comments']);
  110.  
  111. // prove if a bot has entered data
  112. $botty = strip_tags($_POST['botty']);
  113.  
  114. if($botty != NULL){
  115. echo "barghh! darn bot!";
  116. }
  117.  
  118.  
  119. // the email address where the script will email the form results to
  120.  
  121. // where the email will look like it is sent from
  122.  
  123. $subject = "Email from your website";
  124.  
  125. $body = "Email from your website" . "
  126.  
  127. ";
  128. $body .= "Name: " . $FirstName . $LastName . "
  129. ";
  130. $body .= "Email: " . $email . "
  131. ";
  132. $body .= "Newsletter: " . $subscribe . "
  133. ";
  134. $body .= "Comments: " . $comments . "
  135. ";
  136.  
  137. $headers = "From: $from" . "
  138. ";
  139. $headers .= "Reply-To: $from" . "
  140. ";
  141. $headers .= "Return-Path: $from" . "
  142. ";
  143.  
  144. // mail(to,subject,body,headers);
  145.  
  146. $isMailed = mail($to, $subject, $body, $headers);
  147.  
  148. if($isMailed){
  149. echo "Thank you for your inquery, " . $FirstName . " " . $LastName . " We will get back to you shortly.";
  150. }else{
  151. echo "There seemed to be a problem";
  152. }
  153.  
  154. ?>

以上是关于简单的PHP联系人表单+jscript验证+anti-bot的主要内容,如果未能解决你的问题,请参考以下文章