1. Principle
Each picture in the array corresponds to a value, a random value, gets and saved to $_session,-> gets the picture of the random value,-> __file__ output picture,-> browser validation
2. Code
captcha.php
<?PHPSession_Start();//Picture Correspondence$picArray=Array( ' pic00 ' = ' dog ', ' pic01 ' = ' cat ', ' pic02 ' and ' fish ', ' pic03 ' and ' bird ');//Random Value$index=Mt_rand(0,3);$value=$picArray[' Pic0 '.$index];$_session[' authcode '] =$value;$filename=dirname(__file__).‘. /img/pic0 '.$index.‘. jpg;//echo $filename;$content=file_get_contents($filename);Header(' Content-type:image/jpg ');Echo $content;
form.php
<?PHPif(isset($_request[' Authcode '])) { Session_Start(); //Judging if(Strtolower($_request[' authcode '] = =Strtolower($_session[' Authcode ']))) { Echo' Congratulations on your typing right! ‘; } Else { Echo' Input failed! ‘; } Exit();}?> for= "Authcode" > Please enter the contents of the Picture: </label> <input type= "text" name= "Authcode" id= "Authcode"/> </p> <p><input type= "Submit" value= "Submit"/></p></form></body>
PHP Image Verification Code