{"id":14,"date":"2010-07-23T05:34:30","date_gmt":"2010-07-23T05:34:30","guid":{"rendered":"http:\/\/localhost\/test\/page\/blog\/?p=14"},"modified":"2015-10-07T15:56:21","modified_gmt":"2015-10-07T10:26:21","slug":"control-spam-from-forms","status":"publish","type":"post","link":"https:\/\/www.create-dynamic.com\/blog\/14\/phptags\/control-spam-from-forms\/","title":{"rendered":"Control Spam from Forms, Control form spams"},"content":{"rendered":"<p>In  this page you can be explained how the forms built with spam protection.<br \/>\nToday lot of bots,hackers came. So we have to protect the site from hackers at maximum.<br \/>\nFew listed here are best technique to protect your site from spam or bots.<\/p>\n<p>Why we used all these techniques?<br \/>\nNow there are many automated form filling tools available in online.Once they plugged into the browser they have capable of automatically fill the information and submitted. It will ruin our database.Also it take time to delete. So it leads to problem.<\/p>\n<p>There are many programmers or black hat hacker available now.They can program for specific application that will able to do automated tasks.<br \/>\n<strong>Method 1<\/strong><\/p>\n<h3>Captcha<\/h3>\n<div class=\"div_code\">\n<pre><code class=\"php\">\"Completely Automated Public Turing test to tell Computers and Humans Apart.\" is best method to reduce the spams.<\/code><\/pre>\n<\/div>\n<p><strong>Method 2<\/strong><\/p>\n<h3>By hidden Fields<\/h3>\n<p>Robots are trying to fill the every form fields.So it may chance to fill  the hidden fields or invisible fields where users are not able to do normally.<br \/>\nIt can possible by users to change the default value of any thing that was hidden in a page.I will show you later.<\/p>\n<p>Add any hidden fields like below<\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">&lt;input type=\"hidden\" name=\"sites\" value=\"\"\/&gt;\r\nOR\r\n&lt;span style=\"display:none\"&gt;&lt;input type=\"text\" name=\"check\" value=\"\" \/&gt;&lt;\/span&gt;<\/code><\/pre>\n<\/div>\n<p>Humans are not able to see the hidden text box normally.<br \/>\nSo when you submit the form you can check the hidden fields like below.<\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">if(isset($_POST['sites']) &amp;&amp; trim($_POST['sites'])=='') {\r\n\/\/do something\r\n}else\r\n\/\/Pass this to error or thankyou page.<\/code><\/pre>\n<\/div>\n<p>By checking above fields to null we confirm that are human only and save the application or do something<br \/>\n<!--more--><br \/>\n<strong>Method 3<\/strong><\/p>\n<h3>By Dynamic varying fields names<\/h3>\n<p>This can greatly reduce the spams.I will demonstrate how to do now?<br \/>\n<em><strong>Create a file name file1.php<\/strong><\/em><br \/>\ncopy the below snippet and paste in your files<\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">&lt;?php\r\nsession_start() ;\r\n$_SESSION['txt']['fname']='fname'.md5(rand(2,10));\r\n$_SESSION['txt']['lname']='lname'.md5(rand(2,10));\r\n?&gt;\r\n&lt;form action=\"page2.php\" method=\"post\" name=\"dynamicfields_name\"&gt;\r\n&lt;div&gt;First Name&lt;\/div&gt;&lt;input type=\"text\" name=\"&lt;?php echo $_SESSION['txt']['fname'];?&gt;\" id=\"fname\" value=\"\"\/&gt;&lt;div&gt;Last Name &lt;\/div&gt;&lt;div&gt;&lt;input type=\"text\" name=\"&lt;?php echo $_SESSION['txt']['lname'];?&gt;\" id=\"lname\" value=\"\"\/  &gt;&lt;\/div&gt;&lt;h4 &gt;&lt;\/h4&gt;\r\n&lt;div&gt;&lt;input type=\"submit\" value=\"Submit\"\/&gt;&lt;\/div&gt;\r\n&lt;\/form&gt;<\/code><\/pre>\n<\/div>\n<p>Browser output would be like this. Its a random number generated. so we cannot predict that.So it is secure now.<\/p>\n<div class=\"div_code\">\n<pre><code class=\"html\">&lt;input type=\"text\" name=\"fname3ef815416f775098fe977004015c6193\" value=\"\" \/&gt;<\/code><\/pre>\n<\/div>\n<p><em><strong>Create action page like page2.php<\/strong><\/em><br \/>\ncopy the below snippet and paste in your file<\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">&lt;?php\r\nsession_start() ;\r\nif(count($_SESSION['txt'] )&gt;0)\r\nforeach($_SESSION['txt'] as $key=&gt;$value)    {\r\n$$key = trim($_POST[$value]) ;\r\n}\r\n?&gt;<\/code><\/pre>\n<\/div>\n<p>\/\/Now $fname equal  to the random session textbox name. You cannot predict the name of the input name.  So initially we store the random field names in session with known key.<\/p>\n<p>Every refresh of browser would save the random field names in session key like fname,lname.<\/p>\n<p>Now we have an session array of key and value as the dynamic field names.<br \/>\nOnce we do the foreach, we can get the value as random field names and key as actual field names<\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">foreach($_SESSION['txt'] as $key=&gt;$value)    {\r\n$$key = trim($_POST[$value]);\r\n}\r\n\/\/echo $fname;\r\n\/\/echo $lname;\r\n<\/code><\/pre>\n<\/div>\n<p><strong>Method 4<\/strong><\/p>\n<h3>IP Restriction<\/h3>\n<p>Restrict the spam ip to protect your site.<\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">1.)There are some web services available which provide spam ip. From which you can restrict spammers to post.Also web services return country code from that you can ban or allow the country.\r\n2.)Store the spam ip in your database and restrict them to post.\r\n3.)Every one in the world could not have the static ip address. There may be possible to have shared ip. Some small company or individual have shared ip. Ip is changing daily or when restart the modem.So it may not visible to them if you ban ip address. Try to examine the ip which spam daily and ban it if you cant able manage it.\r\n4.)Some Web services available to check some factors in a posted content and return true or false.From which you can store in a database or other purpose Ex: Akismet\r\n5.)You can restrict to post if there was a link or some headers.\r\n6.)I was experienced some problem related to search engine. Some search engine act as a bots and post the data continuously.Also it impair the database or files if there was weakly coded.Every search engine was not genuine.\r\n<\/code><\/pre>\n<\/div>\n<p><strong>Using php:<\/strong><\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">$banip[]='xxx.xxx.xxx.xx1';\r\n$banip[]='xxx.xxx.xxx.xx2';\r\nif (in_array($_SERVER['REMOTE_ADDR'],$banip))\r\ndie ( \"Permission denied!\" );\r\nif ( in_array($_SERVER['REMOTE_ADDR'],$banip )) {\r\nheader(\"HTTP\/1.1 403 Forbidden\");\r\nexit; }\r\n<\/code><\/pre>\n<\/div>\n<p><strong>Using Htaccess<\/strong><\/p>\n<div class=\"div_code\">\n<pre><code class=\"htaccess\">order allow,deny\r\ndeny from xxx.xxx.xxx.xx1\r\ndeny from xxx.xxx.xxx.xx2\r\ndeny from xxx.xxx.xxx.xx3\r\nallow from all\r\n<\/code><\/pre>\n<\/div>\n<h3>Conclusions:<\/h3>\n<p>These above methods are effective to handle the spams. It is better to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is many techniques available to secure your form.These are captcha, hidden fields, dynamic varying field names,ip restriction via htacess;php;webservices and form timeouts. We brief the above concepts clearly.Control form spams,control spams, restriction to robots, anti spam,dynamic varying field names to control spam<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[29,32,9,15,20,26,31,30,28,10,27],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-phptags","tag-add-security-to-forms","tag-anti-spam-techniques","tag-control-spam","tag-create-form-with-spam-reduction","tag-dynamic-field-names","tag-form-spam-control","tag-form-spam-protection","tag-honeypots","tag-php-form-validation","tag-restriction-to-robots","tag-spam-check"],"_links":{"self":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":83,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions\/387"}],"wp:attachment":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}