{"id":294,"date":"2009-11-04T17:54:01","date_gmt":"2009-11-04T15:54:01","guid":{"rendered":"http:\/\/www.matteoiammarrone.com\/public\/blog.php\/?p=294"},"modified":"2009-11-19T20:23:49","modified_gmt":"2009-11-19T18:23:49","slug":"controllo-antispam-in-php","status":"publish","type":"post","link":"https:\/\/www.matteoiammarrone.com\/public\/controllo-antispam-in-php\/","title":{"rendered":"Controllo antispam in php"},"content":{"rendered":"<p>Quante volte avreste voluto avere un bellissimo e professionale codice antispam nei vostri scripts in php?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.matteoiammarrone.com\/public\/images\/captcha.png\" alt=\"\" width=\"255\" height=\"100\" \/><\/p>\n<p><span style=\"background-color: #ffffff;\">Ecco a voi come fare.<\/span><\/p>\n<p><span style=\"background-color: #ffffff;\">Svilupperemo una classe e useremo le librerie gd del nostro fidato linguaggio.<\/span><\/p>\n<p>1 ) Create una pagina di nome <strong>antispam.php<\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p><strong> <\/strong><br \/>\n[php]<\/p>\n<blockquote><p><span style=\"font-weight: normal;\"> session_start(); \/\/Avviamo la sessione<\/span><\/p>\n<p><span style=\"font-weight: normal;\">class CaptchaSecurityImages { \/\/Creazione della classe<\/span><\/p>\n<p><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">var $font = &#8217;monofont.ttf&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">function generateCode($characters) {<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">\/* list all possible characters, similar looking characters and vowels have been removed *\/<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$possible = &#8217;23456789bcdfghjkmnpqrstvwxyz&#8217;;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$code = &#8221;;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$i = 0;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">while ($i &lt; $characters) { <\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$i++;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">}<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">return $code;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">}<\/span><\/p>\n<p><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">function CaptchaSecurityImages($width=&#8217;120&#8242;,$height=&#8217;40&#8217;,$characters=&#8217;6&#8242;) {<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$code = $this-&gt;generateCode($characters);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">\/* font size will be 75% of the image height *\/<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$font_size = $height * 0.75;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$image = @imagecreate($width, $height) or die(&#8217;Le librerie gd non sono installate nel tuo php&#8217;);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">\/* set the colours *\/<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$background_color = imagecolorallocate($image, 255, 255, 255);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$text_color = imagecolorallocate($image, 20, 40, 100);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$noise_color = imagecolorallocate($image, 100, 120, 180);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">\/* generate random dots in background *\/<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">for( $i=0; $i&lt;($width*$height)\/3; $i++ ) {<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">}<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">\/* generate random lines in background *\/<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">for( $i=0; $i&lt;($width*$height)\/150; $i++ ) {<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">}<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">\/* create textbox and add text *\/<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$textbox = imagettfbbox($font_size, 0, $this-&gt;font, $code) or die(&#8217;Error in imagettfbbox function&#8217;);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$x = ($width &#8211; $textbox[4])\/2;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$y = ($height &#8211; $textbox[5])\/2;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">imagettftext($image, $font_size, 0, $x, $y, $text_color, $this-&gt;font , $code) or die(&#8217;Error in imagettftext function&#8217;);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">\/* output captcha image to browser *\/<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">header(&#8217;Content-Type: image\/jpeg&#8217;);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">imagejpeg($image);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">imagedestroy($image);<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">$_SESSION[&#8217;security_code&#8217;] = $code;<\/span><\/p>\n<p><span><span style=\"font-weight: normal;\"> <\/span><\/span><span style=\"font-weight: normal;\">}<\/span><\/p>\n<p><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-weight: normal;\">}<\/span><\/p>\n<p><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-weight: normal;\">$width = isset($_GET[&#8217;width&#8217;]) ? $_GET[&#8217;width&#8217;] : &#8217;120&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">$height = isset($_GET[&#8217;height&#8217;]) ? $_GET[&#8217;height&#8217;] : &#8217;40&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">$characters = isset($_GET[&#8217;characters&#8217;]) &amp;&amp; $_GET[&#8217;characters&#8217;] &gt; 1 ? $_GET[&#8217;characters&#8217;] : &#8217;6&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-weight: normal;\">$captcha = new CaptchaSecurityImages($width,$height,$characters); \/\/Stampiamo la classe<\/span><\/p>\n<p><span style=\"font-weight: normal;\">?&gt;[\/php]<\/span><\/p><\/blockquote>\n<p><span style=\"font-weight: normal;\">2 ) Scaricate e caricate sul vostro spazio web anche <a href=\"https:\/\/www.matteoiammarrone.com\/public\/includes\/monofont.ttf\">questo file<\/a><\/span><\/p>\n<p>Esempio di utilizzo<\/p>\n<p><span style=\"font-weight: normal;\"> Codice form html(Pagina <\/span>form.php<span style=\"font-weight: normal;\">):<\/span><\/p>\n<blockquote><p><span style=\"font-weight: normal;\">&lt;form action=&#8221;form.php&#8221; method=&#8221;post&#8221;&gt;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">Nome: &lt;input type=&#8221;text&#8221; name=&#8221;nome&#8221; \/&gt;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">&lt;br&gt;&lt;img src=&#8221;antispam.php?width=100&amp;height=40&amp;characters=5&#8243; \/&gt;&lt;br \/&gt;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">&lt;label for=&#8221;security_code&#8221;&gt;Ricopia il codice che vedi nell&#8217;immagine &lt;\/label&gt;&lt;input id=&#8221;security_code&#8221; name=&#8221;security_code&#8221; type=&#8221;text&#8221; \/&gt;&lt;br \/&gt;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">&lt;br&gt;&lt;input type=&#8221;submit&#8221; name=&#8221;send&#8221; value=&#8221;Invia&#8221;\/&gt;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">&lt;\/form&gt;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">&lt;?php<\/span><\/p>\n<p><span style=\"font-weight: normal;\">if ($_POST[&#8217;send&#8217;]){<\/span><\/p>\n<p><span style=\"font-weight: normal;\">session_start();<\/span><\/p>\n<p><span style=\"font-weight: normal;\">if ($_SESSION[&#8217;security_code&#8217;] == $_POST[&#8217;security_code&#8217;]){<\/span><\/p>\n<p><span style=\"font-weight: normal;\">echo &#8221;Il codice antispam \u00e8 giusto!&#8221;;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">echo $_POST[&#8217;nome&#8217;];<\/span><\/p>\n<p><span style=\"font-weight: normal;\">\/\/RISULTATO POSITIVO<\/span><\/p>\n<p><span style=\"font-weight: normal;\">} else {<\/span><\/p>\n<p><span style=\"font-weight: normal;\">echo &#8221;Il codice antispam che hai inserito \u00e8 errato!&#8221;;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">}<\/span><\/p>\n<p><span style=\"font-weight: normal;\">}<br \/>\n[\/php]<\/span><\/p><\/blockquote>\n<p><span style=\"font-weight: normal;\">Ovviamente, quello sovrastante era solo un banale esempio.<\/span><\/p>\n<p><span style=\"font-weight: normal;\">Avrete capito che il codice di base da inserire tra i tag &lt;form&gt; e &lt;\/form&gt; \u00e8 il seguente:<\/span><\/p>\n<p><strong><\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p><strong><\/p>\n<blockquote><p><span style=\"font-weight: normal;\">&lt;img src=&#8221;antispam.php?width=100&amp;height=40&amp;characters=5&#8243; \/&gt;&lt;br \/&gt;<\/span><\/p>\n<p><span style=\"font-weight: normal;\">&lt;label for=&#8221;security_code&#8221;&gt;Ricopia il codice che vedi nell&#8217;immagine &lt;\/label&gt;&lt;input id=&#8221;security_code&#8221; name=&#8221;security_code&#8221; type=&#8221;text&#8221; \/&gt;<\/span><\/p><\/blockquote>\n<p><span style=\"font-weight: normal;\">e quello da inserire nel php e&#8217; un controllo (if ) di uguaglianza ( == ) tra $_SESSION[&#8217;security_code&#8217;] e $_POST[&#8217;security_code&#8217;].<\/span><\/p>\n<p><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/p>\n<p><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quante volte avreste voluto avere un bellissimo e professionale codice antispam nei vostri scripts in php? Ecco a voi come fare. Svilupperemo una classe e useremo le librerie gd del nostro fidato linguaggio. 1 ) Create una pagina di nome antispam.php [php] session_start(); \/\/Avviamo la sessione class CaptchaSecurityImages { \/\/Creazione della classe var $font =&#8230;<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[532],"tags":[71,73,75,76,70,74,72,35],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-gott-och-blandat","tag-antispam","tag-antispam-check","tag-antispam-con-php","tag-antispam-in-php","tag-captcha","tag-controllo-antispam","tag-matteoiamma","tag-php"],"_links":{"self":[{"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":5,"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/posts\/294\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matteoiammarrone.com\/public\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}