example_form.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. session_start(); // this MUST be called prior to any output including whitespaces and line breaks!
  3. $GLOBALS['DEBUG_MODE'] = 1;
  4. // CHANGE TO 0 TO TURN OFF DEBUG MODE
  5. // IN DEBUG MODE, ONLY THE CAPTCHA CODE IS VALIDATED, AND NO EMAIL IS SENT
  6. $GLOBALS['ct_recipient'] = 'YOU@EXAMPLE.COM'; // Change to your email address! Make sure DEBUG_MODE above is 0 for mail to send!
  7. $GLOBALS['ct_msg_subject'] = 'Securimage Test Contact Form';
  8. ?>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head>
  12. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  13. <title>Securimage Example Form</title>
  14. <style type="text/css">
  15. <!--
  16. div.error { display: block; color: #f00; font-weight: bold; font-size: 1.2em; }
  17. span.error { display: block; color: #f00; font-style: italic; }
  18. .success { color: #00f; font-weight: bold; font-size: 1.2em; }
  19. form label { display: block; font-weight: bold; }
  20. fieldset { width: 90%; }
  21. legend { font-size: 24px; }
  22. .note { font-size: 18px;
  23. -->
  24. </style>
  25. </head>
  26. <body>
  27. <fieldset>
  28. <legend>Example Form</legend>
  29. <p class="note">
  30. This is an example PHP form that processes user information, checks for errors, and validates the captcha code.<br />
  31. This example form also demonstrates how to submit a form to itself to display error messages.
  32. </p>
  33. <?php
  34. process_si_contact_form(); // Process the form, if it was submitted
  35. if (isset($_SESSION['ctform']['error']) && $_SESSION['ctform']['error'] == true): /* The last form submission had 1 or more errors */ ?>
  36. <div class="error">There was a problem with your submission. Errors are displayed below in red.</div><br />
  37. <?php elseif (isset($_SESSION['ctform']['success']) && $_SESSION['ctform']['success'] == true): /* form was processed successfully */ ?>
  38. <div class="success">The captcha was correct and the message has been sent! The captcha was solved in <?php echo $_SESSION['ctform']['timetosolve'] ?> seconds.</div><br />
  39. <?php endif; ?>
  40. <form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']) ?>" id="contact_form">
  41. <input type="hidden" name="do" value="contact" />
  42. <p>
  43. <label for="ct_name">Name*:</label>
  44. <?php echo @$_SESSION['ctform']['name_error'] ?>
  45. <input type="text" name="ct_name" size="35" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_name']) ?>" />
  46. </p>
  47. <p>
  48. <label for="ct_email">Email*:</label>
  49. <?php echo @$_SESSION['ctform']['email_error'] ?>
  50. <input type="text" name="ct_email" size="35" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_email']) ?>" />
  51. </p>
  52. <p>
  53. <label for="ct_URL">URL:</label>
  54. <?php echo @$_SESSION['ctform']['URL_error'] ?>
  55. <input type="text" name="ct_URL" size="35" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_URL']) ?>" />
  56. </p>
  57. <p>
  58. <label for="ct_message">Message*:</label>
  59. <?php echo @$_SESSION['ctform']['message_error'] ?>
  60. <textarea name="ct_message" rows="12" cols="60"><?php echo htmlspecialchars(@$_SESSION['ctform']['ct_message']) ?></textarea>
  61. </p>
  62. <p>
  63. <?php
  64. // show captcha HTML using Securimage::getCaptchaHtml()
  65. require_once 'securimage.php';
  66. $options = array();
  67. $options['input_name'] = 'ct_captcha'; // change name of input element for form post
  68. if (!empty($_SESSION['ctform']['captcha_error'])) {
  69. // error html to show in captcha output
  70. $options['error_html'] = $_SESSION['ctform']['captcha_error'];
  71. }
  72. echo Securimage::getCaptchaHtml($options);
  73. ?>
  74. </p>
  75. <p>
  76. <br />
  77. <input type="submit" value="Submit Message" />
  78. </p>
  79. </form>
  80. </fieldset>
  81. </body>
  82. </html>
  83. <?php
  84. // The form processor PHP code
  85. function process_si_contact_form()
  86. {
  87. $_SESSION['ctform'] = array(); // re-initialize the form session data
  88. if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
  89. // if the form has been submitted
  90. foreach($_POST as $key => $value) {
  91. if (!is_array($key)) {
  92. // sanitize the input data
  93. if ($key != 'ct_message') $value = strip_tags($value);
  94. $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
  95. }
  96. }
  97. $name = @$_POST['ct_name']; // name from the form
  98. $email = @$_POST['ct_email']; // email from the form
  99. $URL = @$_POST['ct_URL']; // url from the form
  100. $message = @$_POST['ct_message']; // the message from the form
  101. $captcha = @$_POST['ct_captcha']; // the user's entry for the captcha code
  102. $name = substr($name, 0, 64); // limit name to 64 characters
  103. $errors = array(); // initialize empty error array
  104. if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
  105. // only check for errors if the form is not in debug mode
  106. if (strlen($name) < 3) {
  107. // name too short, add error
  108. $errors['name_error'] = 'Your name is required';
  109. }
  110. if (strlen($email) == 0) {
  111. // no email address given
  112. $errors['email_error'] = 'Email address is required';
  113. } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
  114. // invalid email format
  115. $errors['email_error'] = 'Email address entered is invalid';
  116. }
  117. if (strlen($message) < 20) {
  118. // message length too short
  119. $errors['message_error'] = 'Your message must be longer than 20 characters';
  120. }
  121. }
  122. // Only try to validate the captcha if the form has no errors
  123. // This is especially important for ajax calls
  124. if (sizeof($errors) == 0) {
  125. require_once dirname(__FILE__) . '/securimage.php';
  126. $securimage = new Securimage();
  127. if ($securimage->check($captcha) == false) {
  128. $errors['captcha_error'] = 'Incorrect security code entered<br />';
  129. }
  130. }
  131. if (sizeof($errors) == 0) {
  132. // no errors, send the form
  133. $time = date('r');
  134. $message = "A message was submitted from the contact form. The following information was provided.<br /><br />"
  135. . "<em>Name: $name</em><br />"
  136. . "<em>Email: $email</em><br />"
  137. . "<em>URL: $URL</em><br />"
  138. . "<em>Message:</em><br />"
  139. . "<pre>$message</pre>"
  140. . "<br /><br /><em>IP Address:</em> {$_SERVER['REMOTE_ADDR']}<br />"
  141. . "<em>Time:</em> $time<br />"
  142. . "<em>Browser:</em> {$_SERVER['HTTP_USER_AGENT']}<br />";
  143. $message = wordwrap($message, 70);
  144. if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
  145. // send the message with mail()
  146. mail($GLOBALS['ct_recipient'], $GLOBALS['ct_msg_subject'], $message, "From: {$GLOBALS['ct_recipient']}\r\nReply-To: {$email}\r\nContent-type: text/html; charset=UTF-8\r\nMIME-Version: 1.0");
  147. }
  148. $_SESSION['ctform']['timetosolve'] = $securimage->getTimeToSolve();
  149. $_SESSION['ctform']['error'] = false; // no error with form
  150. $_SESSION['ctform']['success'] = true; // message sent
  151. } else {
  152. // save the entries, this is to re-populate the form
  153. $_SESSION['ctform']['ct_name'] = $name; // save name from the form submission
  154. $_SESSION['ctform']['ct_email'] = $email; // save email
  155. $_SESSION['ctform']['ct_URL'] = $URL; // save URL
  156. $_SESSION['ctform']['ct_message'] = $message; // save message
  157. foreach($errors as $key => $error) {
  158. // set up error messages to display with each field
  159. $_SESSION['ctform'][$key] = "<span class=\"error\">$error</span>";
  160. }
  161. $_SESSION['ctform']['error'] = true; // set error floag
  162. }
  163. } // POST
  164. }
  165. $_SESSION['ctform']['success'] = false; // clear success value after running