{"id":1236,"date":"2018-01-19T19:22:35","date_gmt":"2018-01-19T19:22:35","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/visual-verification-in-php\/"},"modified":"2018-01-19T19:24:38","modified_gmt":"2018-01-19T19:24:38","slug":"visual-verification-in-php","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/visual-verification-in-php\/","title":{"rendered":"Visual Verification in PHP"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Brett Patterson<\/div>\n<div class=\"\">on March 2, 2006<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>\u00bfVerification?<\/h2>\n<div class=\"articlePara\">\nMany topics on the <a href=\"..\/forum\/index.html\">discussion forums<\/a> deal with the verification of form data. Often it is checked to determine whether or not the submission is from a user or from a &#8220;bot&#8221;, if the email address entered is a valid address, or if all the information that is required has been entered into the form.  While it&#8217;s fairly easy to check to see if a form field is empty, determining if the posted information came from a real human is another task altogether. Most forms now include image verification for just this reason. This article will demonstrate how to create a simplified image verification system.\n<\/div>\n<h2>Random strings, or random characters?<\/h2>\n<div class=\"articlePara\">\nThere are two different ways that we can go about doing this.  We can use a set of pre-made sentences and randomly choose characters from those sentences, or we can randomly generate an alpha-numeric string.  Both methods are going to be explained and explored in our code. Let&#8217;s get to it!\n<\/div>\n<h2>Defining some things<\/h2>\n<div class=\"articlePara\">\nWith our verification, we will use &#8220;random&#8221; colors for text and backgrounds.  Also, we need to define a maximum length of characters that the &#8220;random&#8221; string will be.  We also need to define what font we&#8217;re using and where to find it (see the next section for more information on that subject).<\/p>\n<p>To make our code more portable, we&#8217;ll be using a function to create the image, so we can start by declaring the function and then our variables:\n<\/p><\/div>\n<div class=\"phpcode\">\n<div class=\"codetitle\">Code:<\/div>\n<pre class=\"code\">&lt;?php\n<span class=\"rsvd\">function<\/span> gen_rand_img()\n{\n\t<span class=\"com\">\/**\n\t *\tWe define some static items here\n\t *\/\n\n\t ## Define our random sentences<\/span>\n\t<span class=\"var\">$sentences<\/span> = <span class=\"rsvd\">array<\/span>(\n\t\t\t<span class=\"str\">'This is a random string'<\/span>,\n\t\t\t<span class=\"str\">'Lorum Ipsum Doler sit amet'<\/span>,\n\t\t\t<span class=\"str\">'The quick brown fox jumps over the lazy dog'<\/span>,\n\t\t\t<span class=\"str\">'Kick the tires and light the fires Big Daddy'<\/span>\n\t\t\t);\n\n\t<span class=\"com\">## Define font file<\/span>\n\t<span class=\"func\">putenv<\/span>(<span class=\"str\">'GDFONTPATH='<\/span>.<span class=\"func\">realpath<\/span>(<span class=\"str\">'.'<\/span>)); <span class=\"com\">\/\/ Set GD font path to this directory<\/span>\n\t<span class=\"var\">$font<\/span> = <span class=\"str\">'ProggyCleanSZBP'<\/span>; <span class=\"com\">\/\/ Leave extension off\n\n\t## Define some random colors\n\t## http:\/\/december.com\/html\/spec\/colordec.html<\/span>\n\t<span class=\"var\">$bg_colors<\/span> = <span class=\"rsvd\">array<\/span>( <span class=\"com\">## BACKGROUND COLORS<\/span>\n\t\t\t<span class=\"str\">'176.196.222'<\/span>, <span class=\"com\">\/\/ Blue<\/span>\n\t\t\t<span class=\"str\">'204.153.204'<\/span>, <span class=\"com\">\/\/ Purple<\/span>\n\t\t\t<span class=\"str\">'204.204.204'<\/span>, <span class=\"com\">\/\/ Gray<\/span>\n\t\t\t<span class=\"str\">'227.81.82'<\/span>, <span class=\"com\">\/\/ Red<\/span>\n\t\t\t<span class=\"str\">'150.200.162'<\/span>\t <span class=\"com\">\/\/ Green<\/span>\n\t\t\t);\n\t<span class=\"var\">$font_colors<\/span> = <span class=\"rsvd\">array<\/span>( <span class=\"com\">## FONT COLORS<\/span>\n\t\t\t<span class=\"str\">'0.0.139'<\/span>, <span class=\"com\">\/\/ Blue<\/span>\n\t\t\t<span class=\"str\">'104.34.139'<\/span>, <span class=\"com\">\/\/ Purple<\/span>\n\t\t\t<span class=\"str\">'79.79.79'<\/span>, <span class=\"com\">\/\/ Gray<\/span>\n\t\t\t<span class=\"str\">'128.0.0'<\/span>, <span class=\"com\">\/\/ Red<\/span>\n\t\t\t<span class=\"str\">'59.94.15'<\/span>\t <span class=\"com\">\/\/ Green<\/span>\n\t\t\t);\n\n\t<span class=\"var\">$img_width<\/span> = <span class=\"num\">150<\/span>; <span class=\"com\">## Image Width<\/span>\n\t<span class=\"var\">$img_height<\/span> = <span class=\"num\">75<\/span>; <span class=\"com\">## Image height<\/span>\n\t<span class=\"var\">$fnt_size<\/span> = <span class=\"num\">24<\/span>;  <span class=\"com\">## Font-Size<\/span>\n\t<span class=\"var\">$let_space<\/span> = <span class=\"num\">10<\/span>; <span class=\"com\">## Letter Spacing<\/span>\n\t<span class=\"var\">$str_length<\/span> = <span class=\"num\">6<\/span>; <span class=\"com\">## Length of Random String\n\t\/**************\/<\/span>\n?&gt;<\/pre>\n<\/div>\n<h2>Colors Galore!!<\/h2>\n<div class=\"articlePara\">We will now define some colors for the font and background of our image.  We use a decimal separated value of RGB values.  You can define your own colors, but they have to be RGB values.  A large list of colors can be found <a href=\"http:\/\/december.com\/html\/spec\/colordec.html\" target=\"_blank\">here<\/a>.  Be careful when defining colors so that you don&#8217;t use colors that are too similar.  You want to avoid having text that is unreadable or too difficult to read, so pick your colors wisely.<\/div>\n<h2>What Font??<\/h2>\n<div class=\"articlePara\">\nWe will now define what font we&#8217;re using.  I chose to use ProggyCleanSZBP as the font for reference because it puts slashes through zeros.  It helps to differentiate between 0 (zero) and O (capitol &#8220;Oh&#8221;).  You can download this font (for free) from its home page at <a href=\"http:\/\/www.proggyfonts.com\/\" target=\"_blank\">Proggy_Fonts.com<\/a>.  You can choose one from there, or just use your own.  Just make sure it&#8217;s a TTF (True Type Font) and it&#8217;s placed in the same directory as this script.<\/p>\n<p>We define the font path to be used with GD by getting the <em>realpath<\/em> to our current directory.  Then, we define our font-name (which is the name of the font file).  We will use it later, but these steps are required.  Now how about those &#8220;random&#8221; strings?\n<\/div>\n<h2>Generating a Random String<\/h2>\n<div class=\"articlePara\">We need to define random strings to use in the image.  Once we get this part ready, it&#8217;s on to generating the image. We will write one function to create our random string, and we&#8217;ll use a series of functions to both randomize the string and randomly select a portion of that string.  The code is fairly simple and straight-forward:<\/div>\n<div class=\"phpcode\">\n<div class=\"codetitle\">Code:<\/div>\n<pre class=\"code\">&lt;?php\n\t<span class=\"rsvd\">function<\/span> gen_random_string(<span class=\"var\">$length<\/span>=<span class=\"num\">5<\/span>, <span class=\"var\">$str<\/span>=<span class=\"str\">''<\/span>)\n\t{\n\t\t<span class=\"rsvd\">for<\/span>(<span class=\"var\">$i<\/span>=<span class=\"num\">1<\/span>; <span class=\"var\">$i<\/span>&lt;=<span class=\"var\">$length<\/span>; <span class=\"var\">$i<\/span>++)\n\t\t{\n\t\t\t<span class=\"var\">$ord<\/span> = <span class=\"func\">rand<\/span>(<span class=\"num\">48<\/span>, <span class=\"num\">90<\/span>);\n\n\t\t\t<span class=\"rsvd\">if<\/span>( ( (<span class=\"var\">$ord<\/span>&gt;=<span class=\"num\">48<\/span>) &amp;&amp; (<span class=\"var\">$ord<\/span>&lt;=<span class=\"num\">57<\/span>) ) || ( (<span class=\"var\">$ord<\/span>&gt;=<span class=\"num\">65<\/span>) &amp;&amp; (<span class=\"var\">$ord<\/span>&lt;=<span class=\"num\">90<\/span>) ) )\n\t\t\t{\n\t\t\t\t<span class=\"var\">$str<\/span> .= <span class=\"func\">chr<\/span>(<span class=\"var\">$ord<\/span>);\n\t\t\t}\n\t\t\t<span class=\"rsvd\">else<\/span>\n\t\t\t{\n\t\t\t\t<span class=\"var\">$str<\/span> .= gen_random_string(<span class=\"num\">1<\/span>);\n\t\t\t}\n\t\t}\n\t\t<span class=\"rsvd\">return<\/span> <span class=\"var\">$str<\/span>;\n\t}\n\n\t<span class=\"com\">## Random String or Random Sentence?<\/span>\n\t<span class=\"var\">$str_snt<\/span> = <span class=\"func\">rand<\/span>(<span class=\"num\">0<\/span>,<span class=\"num\">1<\/span>); <span class=\"com\">\/\/ 0: sentences, 1: random string<\/span>\n\n\t<span class=\"rsvd\">if<\/span>(<span class=\"var\">$str_snt<\/span> == <span class=\"num\">0<\/span>)\n\t{\n\t\t<span class=\"var\">$rnd_str<\/span> = <span class=\"func\">str_replace<\/span>(<span class=\"str\">' '<\/span>, <span class=\"str\">''<\/span>, <span class=\"func\">strtoupper<\/span>(<span class=\"var\">$sentences<\/span>[<span class=\"func\">rand<\/span>(<span class=\"num\">0<\/span>,<span class=\"func\">count<\/span>(<span class=\"var\">$sentences<\/span>)-<span class=\"num\">1<\/span>)]));\n\t\t<span class=\"var\">$rnd_str<\/span> = <span class=\"func\">substr<\/span>(<span class=\"var\">$rnd_str<\/span>, <span class=\"func\">rand<\/span>(<span class=\"num\">0<\/span>,<span class=\"func\">strlen<\/span>(<span class=\"var\">$rnd_str<\/span>)-<span class=\"var\">$str_length<\/span>), <span class=\"var\">$str_length<\/span>);\n\t}\n\t<span class=\"rsvd\">else<\/span>\n\t{\n\t\t<span class=\"var\">$rnd_str<\/span> = gen_random_string(<span class=\"var\">$str_length<\/span>);\n\t}\n?&gt;<\/pre>\n<\/div>\n<h2>What&#8217;s going on?<\/h2>\n<div class=\"articlePara\">\nWith the above code, first we&#8217;re randomizing the string to be either a sentence or alpha-numeric.  Once that is &#8220;decided&#8221; for us by our script, we either generate a random string (with our function) or we select a random sub-string of a random sentence to show.  Now we&#8217;ll delve into what our function does.<\/p>\n<p>We start off by defining the length we want (with a default of 5) and a default string variable.  Doing this helps in two ways:<\/p>\n<ul>\n<li>We can also make this recursive, as we&#8217;ll explain later. <\/li>\n<li>It has expandability<\/li>\n<\/ul>\n<p>When we call the function it loops for as many times as we choose.  For each loop, it generates a random number between 48 and 90.  Then, it checks to make sure that the value it generated is a valid ordinal for an alpha-numeric character.  Those ordinal values are 48 &#8211; 57 (numeric) and 65 &#8211; 90 (alphabetic).<br \/>\nIf the random number is not between those values, it runs itself again (recursively) so that we get a proper valid string.<\/p>\n<p>The sentence version is a bit more complicated.  This one does the following (in this order):<\/p>\n<ol>\n<li>Generates a random key for use with the <span class=\"var\">$sentences<\/span> array<\/li>\n<li>Gets the sentence that is bound to the random key that we generated above<\/li>\n<li>Converts the string to all UPPERCASE letters<\/li>\n<li>Replaces all spaces with no-space so we get a long string of characters<\/li>\n<li>Stores the above string into a variable called <span class=\"var\">$rand_str<\/span><\/li>\n<\/ol>\n<p>The next line that gives us the final random string is a little tricky.  So let&#8217;s break it down, one by one:\n<\/p><\/div>\n<div class=\"phpCode\">\n<div class=\"codetitle\">Code:<\/div>\n<pre class=\"code\"><span class=\"var\">$rnd_str<\/span> = <span class=\"func\">substr<\/span>(<span class=\"var\">$rnd_str<\/span>, <span class=\"func\">rand<\/span>(<span class=\"num\">0<\/span>,<span class=\"func\">strlen<\/span>(<span class=\"var\">$rnd_str<\/span>)-<span class=\"var\">$str_length<\/span>), <span class=\"var\">$str_length<\/span>);<\/pre>\n<\/div>\n<div class=\"articlePara\">\nWorking from the inside out, the first step generates the length of our random string.  Then the rand() function generates a random number from 0 (first character) to <span class=\"var\">$str_length<\/span> characters from the end.  Our first sentence would give:<br \/><i><b>THISISARANDOMSTRING<\/b><\/i> [String Length: 19] &#8211; [$str_length: 6] = 13<\/p>\n<p>So we generate a random number from 0 to 13, but we do it in a dynamic way so that we can use any sentence we want.  Then it&#8217;s just a matter of cutting the string <span class=\"var\">$rnd_str<\/span> from its random starting point (evaluated above) and spanning the next <span class=\"var\">$str_length<\/span> characters.\n<\/div>\n<h2>So How About That Image?<\/h2>\n<div class=\"articlePara\">\nNow that we have our random string in the variable <span class=\"var\">$rnd_str<\/span> we can finally write the code to create the image.\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>While it&#8217;s fairly easy to check to see if a form field is empty, determining if the posted information came from a real human is another task<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1236","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1236"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1236\/revisions"}],"predecessor-version":[{"id":3137,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1236\/revisions\/3137"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}