PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/captcha/examples/traditional-api/traditional-api-php-form-captcha-example/process-form.php

https://bitbucket.org/Pons-bdreamz/anubavam_task
PHP | 98 lines | 65 code | 18 blank | 15 comment | 16 complexity | be8d3d08d981a8f817a3e560133e6152 MD5 | raw file
  1. <?php
  2. session_start();
  3. require("botdetect.php");
  4. $form_page = "index.php";
  5. $view_page = "messages.php";
  6. // directly accessing this script is an error
  7. if (!$_SERVER['REQUEST_METHOD'] == "POST") {
  8. header("Location: ${form_page}");
  9. exit;
  10. }
  11. // sumbitted data
  12. $name = $_REQUEST['Name'];
  13. $email = $_REQUEST['Email'];
  14. $message = $_REQUEST['Message'];
  15. $form_page = $form_page . "?Name=" . urlencode($name) . "&Email=" . urlencode($email) . "&Message=" . urlencode($message);
  16. // total form validation result
  17. $isPageValid = true;
  18. // Captcha validation
  19. $FormCaptcha = new Captcha("FormCaptcha");
  20. $FormCaptcha->UserInputID = "CaptchaCode";
  21. if (!$FormCaptcha->IsSolved) {
  22. $isHuman = $FormCaptcha->Validate();
  23. $isPageValid = $isPageValid && $isHuman;
  24. $form_page = $form_page . "&CaptchaCodeValid=" . $isHuman;
  25. }
  26. // name validation
  27. $isNameValid = ValidateName($name);
  28. $isPageValid = $isPageValid && $isNameValid;
  29. $form_page = $form_page . "&NameValid=" . $isNameValid;
  30. // email validation
  31. $isEmailValid = ValidateEmail($email);
  32. $isPageValid = $isPageValid && $isEmailValid;
  33. $form_page = $form_page . "&EmailValid=" . $isEmailValid;
  34. // message validation
  35. $isMessageValid = ValidateMessage($message);
  36. $isPageValid = $isPageValid && $isMessageValid;
  37. $form_page = $form_page . "&MessageValid=" . $isMessageValid;
  38. if (!$isPageValid) {
  39. // form validation failed, show error message
  40. header("Location: ${form_page}");
  41. exit;
  42. }
  43. // keep a collection of submitted valid messages in Session state
  44. SaveMessage($name, $email, $message);
  45. $FormCaptcha->Reset(); // each message requires a new Captcha challenge
  46. header("Location: ${view_page}");
  47. exit;
  48. // name validation
  49. function ValidateName($name) {
  50. $result = false;
  51. if (strlen($name) > 2 && strlen($name) < 30) {
  52. $result = true;
  53. }
  54. return $result;
  55. }
  56. // email validaton
  57. function ValidateEmail($email) {
  58. $result = false;
  59. if (strlen($email) < 5 || strlen($email) > 100) {
  60. $result = false;
  61. } else {
  62. $result = (1 == preg_match('/^(.+)@(.+)\.(.+)$/', $email));
  63. }
  64. return $result;
  65. }
  66. // message validation
  67. function ValidateMessage($message) {
  68. $result = false;
  69. if (strlen($message) > 2 && strlen($message) < 255) {
  70. $result = true;
  71. }
  72. return $result;
  73. }
  74. // data storage
  75. function SaveMessage($name, $email, $message) {
  76. // we want to keep the example code simple, so we'll store the messages in Session state despite it being unfit for real-world use in such scenarios;
  77. // using a database or another appropriate persistence medium would complicate the example code
  78. $_SESSION['Message_' . strtolower(md5(uniqid(mt_rand(), true)))] = htmlentities($name) . ' (' . htmlentities($email) . ') says: ' . htmlentities($message);
  79. }
  80. ?>