PHP code to verify the CAPTCHA:
A four-digit verification code is randomly generated using random numbers.
1<?PHP2 3 Session_Start();4 $arr=Array(5' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' X ',6' Y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '7 );8 $rand= "";9 for($i= 1;$i<=4;$i++){Ten $rand.=$arr[Rand(0,Count($arr)-1)]; One } A $_session[' check_pic '] =$rand; - //Create a picture - $im= Imagecreatetruecolor (100,30); the //Generate color , when the first call to generate a color method, is to generate a background color - $BG= Imagecolorallocate ($im, 0,0,0); - //The second call to this method is the ability to generate the text above the picture or the color of other styles - $te= Imagecolorallocate ($im, 255,255,255); + - //create text on top of a picture +Imagestring ($im,Rand(1,5),Rand(3,70),Rand(3,15),$rand,$te); A //to take PHP as a picture output, you must give the file a header declaration at - Ob_clean(); - Header("Content-type:image/jpeg"); - //Final image Generation -Imagejpeg ($im); - in?>
Code to verify that the captcha is correct:
1 <HTML>2 <Head>3 <Metahttp-equiv= "Content-type" 4 5 content= "Text/html;charset=utf-8"/>6 </Head>7 <Body>8 9 <?PHPTen One session_start (); A if (isset ($_post[' check ')) { - if ($_post[' check '] = = $_session[' Check_pic ']) { - echo "Verification success"; the }else{ - echo "Verification failed"; - } - } + ?> - + <formAction= "check2.php"Method= "POST"> A <inputtype= "text"name= "Check"/> at <imgsrc= "check1.php"alt=""onclick= "refreshimg ()" - - ID= "Chk"style= "Cursor:pointer"/> - <BR/> - - <inputtype= "Submit"value= "Submit"/> in </form> - <Script> to functionrefreshimg () { + //avoid the same page that the browser thinks you have been accessing, so use random numbers to pass a - the value to make it think that the access is a different page * varRand=Math.Round (Math.random ()*10000); $ varchk=document.getElementById ("chk");Panax Notoginseng chk.src= "check1.php?num="+Rand; - } the </Script>