{"id":1107,"date":"2023-08-18T18:07:24","date_gmt":"2023-08-18T12:37:24","guid":{"rendered":"https:\/\/vining.in\/blog\/?p=1107"},"modified":"2023-08-18T18:07:24","modified_gmt":"2023-08-18T12:37:24","slug":"prevent-php-web-contact-form-spam","status":"publish","type":"post","link":"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/","title":{"rendered":"Prevent PHP web contact form spam"},"content":{"rendered":"<p><strong>The problem<\/strong>: I have a very simple PHP contact form on my business&#8217;s website. It has worked great for years, but in the last week has been hacked. I now receive hundreds of contact form submissions a day with no comments, they only have (apparently valid) email addresses, and a string of characters in the name field (<strong>like &#8220;58ee8b52eef46&#8221;<\/strong>).<\/p>\n<p>I have tried several techniques to prevent this spam, and they either break my php form, or they don&#8217;t prevent the spam. If possible I would like a solution that does NOT require a Captcha distorted text test and does NOT require all fields of the form to be filled.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\nif(isset($_POST['email'])) {\r\n  $email_to = \"myemail@example.com\";\r\n  $email_subject = \"website form submission\";\r\n\r\n  function died($error) {\r\n    echo \"We are very sorry, but there were error(s) found with the form you submitted. \";\r\n    echo \"These errors appear below.&lt;br \/&gt;&lt;br \/&gt;\";\r\n    echo $error.\"&lt;br \/&gt;&lt;br \/&gt;\";\r\n    echo \"Please go back and fix these errors.&lt;br \/&gt;&lt;br \/&gt;\";\r\n    die();\r\n  }\r\n\r\n  if (!isset($_POST['name']) ||\r\n    !isset($_POST['email']) ||\r\n    !isset($_POST['telephone']) ||\r\n    !isset($_POST['comments'])) {\r\n    died('We are sorry, but there appears to be a problem with the form you submitted.');       \r\n  }\r\n\r\n  $name = $_POST['name'];\r\n  $email_from = $_POST['email'];\r\n  $telephone = $_POST['telephone'];\r\n  $comments = $_POST['comments'];\r\n\r\n  $error_message = \"\";\r\n  if(strlen($error_message) &gt; 0) {\r\n    died($error_message);\r\n  }\r\n  $email_message = \"Form details below.\\n\\n\";\r\n\r\n  function clean_string($string) {\r\n    $bad = array(\"content-type\",\"bcc:\",\"to:\",\"cc:\",\"href\");\r\n    return str_replace($bad,\"\",$string);\r\n  }\r\n\r\n  $email_message .= \"Name: \".clean_string($name).\"\\n\";\r\n  $email_message .= \"Email: \".clean_string($email_from).\"\\n\";\r\n  $email_message .= \"Telephone: \".clean_string($telephone).\"\\n\";\r\n  $email_message .= \"Comments: \".clean_string($comments).\"\\n\";\r\n\r\n  $headers = 'From: '.$email_from.\"\\r\\n\" .\r\n             'Reply-To: '.$email_from.\"\\r\\n\" .\r\n             'X-Mailer: PHP\/' . phpversion();\r\n  @mail($email_to, $email_subject, $email_message, $headers);  \r\n?&gt;\r\n\r\nThank you for contacting us. We will be in touch with you soon. You will now be redirected back to example.com.\r\n&lt;META http-equiv=\"refresh\" content=\"2;URL=http:\/\/www.example.com\"&gt;\r\n\r\n&lt;?php\r\n}\r\ndie();\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem: I have a very simple PHP contact form on my business&#8217;s website. It has worked great for years, but in the last week has been hacked. I now receive hundreds of contact form submissions a day with no comments, they only have (apparently valid) email addresses, and a string of characters in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1107","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Prevent PHP web contact form spam - Programmer Code<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prevent PHP web contact form spam - Programmer Code\" \/>\n<meta property=\"og:description\" content=\"The problem: I have a very simple PHP contact form on my business&#8217;s website. It has worked great for years, but in the last week has been hacked. I now receive hundreds of contact form submissions a day with no comments, they only have (apparently valid) email addresses, and a string of characters in the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/\" \/>\n<meta property=\"og:site_name\" content=\"Programmer Code\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vining.in\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-18T12:37:24+00:00\" \/>\n<meta name=\"author\" content=\"codeadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"codeadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/\",\"url\":\"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/\",\"name\":\"Prevent PHP web contact form spam - Programmer Code\",\"isPartOf\":{\"@id\":\"https:\/\/vining.in\/blog\/#website\"},\"datePublished\":\"2023-08-18T12:37:24+00:00\",\"dateModified\":\"2023-08-18T12:37:24+00:00\",\"author\":{\"@id\":\"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e\"},\"breadcrumb\":{\"@id\":\"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vining.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Prevent PHP web contact form spam\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vining.in\/blog\/#website\",\"url\":\"https:\/\/vining.in\/blog\/\",\"name\":\"Programmer Code\",\"description\":\"- Where Developers Learn, Share, &amp; Build Careers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vining.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e\",\"name\":\"codeadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vining.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g\",\"caption\":\"codeadmin\"},\"url\":\"https:\/\/vining.in\/blog\/author\/codeadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Prevent PHP web contact form spam - Programmer Code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/","og_locale":"en_US","og_type":"article","og_title":"Prevent PHP web contact form spam - Programmer Code","og_description":"The problem: I have a very simple PHP contact form on my business&#8217;s website. It has worked great for years, but in the last week has been hacked. I now receive hundreds of contact form submissions a day with no comments, they only have (apparently valid) email addresses, and a string of characters in the [&hellip;]","og_url":"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/","og_site_name":"Programmer Code","article_publisher":"https:\/\/www.facebook.com\/vining.in","article_published_time":"2023-08-18T12:37:24+00:00","author":"codeadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"codeadmin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/","url":"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/","name":"Prevent PHP web contact form spam - Programmer Code","isPartOf":{"@id":"https:\/\/vining.in\/blog\/#website"},"datePublished":"2023-08-18T12:37:24+00:00","dateModified":"2023-08-18T12:37:24+00:00","author":{"@id":"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e"},"breadcrumb":{"@id":"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vining.in\/blog\/prevent-php-web-contact-form-spam\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vining.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Prevent PHP web contact form spam"}]},{"@type":"WebSite","@id":"https:\/\/vining.in\/blog\/#website","url":"https:\/\/vining.in\/blog\/","name":"Programmer Code","description":"- Where Developers Learn, Share, &amp; Build Careers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vining.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vining.in\/blog\/#\/schema\/person\/cda9f07adf3479ac270e2320a914552e","name":"codeadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vining.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/770f5bb2b95d41f98f29e7251fb1873b?s=96&d=mm&r=g","caption":"codeadmin"},"url":"https:\/\/vining.in\/blog\/author\/codeadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts\/1107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/comments?post=1107"}],"version-history":[{"count":1,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts\/1107\/revisions"}],"predecessor-version":[{"id":1109,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/posts\/1107\/revisions\/1109"}],"wp:attachment":[{"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/media?parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/categories?post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vining.in\/blog\/wp-json\/wp\/v2\/tags?post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}