PHP drawing function generates image verification code

Source: Internet
Author: User
PHP drawing function generates image verification code


  Set some important CAPTCHA constantsDefine' Captcha_numchars ',6);// number of characters in Pass-phraseDefine' Captcha_width ', -);//width of imageDefine' Captcha_height ', -);//height of image//Generate the random pass-phrase$pass _phrase=""; for($i=0;$i< Captcha_numchars;$i++) {$pass _phrase. = Chr (Rand( the,122)); }//Store The encrypted pass-phrase in a session variable$_session[' Pass_phrase '] = SHA ($pass _phrase);//Create the image$img= Imagecreatetruecolor (Captcha_width, captcha_height);//Set a white background with black text and gray graphics$BG _color= Imagecolorallocate ($img,255,255,255);// White$text _color= Imagecolorallocate ($img,0,0,0);//Black$graphic _color= Imagecolorallocate ($img, -, -, -);//Dark gray//Fill the backgroundImagefilledrectangle ($img,0,0, Captcha_width, Captcha_height,$BG _color);//Draw some random lines for($i=0;$i<5;$i+ +) {Imageline ($img,0,Rand()% Captcha_height, Captcha_width,Rand()% Captcha_height,$graphic _color); }//Sprinkle in some random dots for($i=0;$i< -;$i+ +) {Imagesetpixel ($img,Rand()% Captcha_width,Rand()% Captcha_height,$graphic _color); }//Draw the Pass-phrase stringImagettftext ($img, -,0,5, Captcha_height-5,$text _color,' Courier New bold.ttf ',$pass _phrase);//Output the image as a PNG using a headerHeader"Content-type:image/png"); Imagepng ($img);// clean upImagedestroy ($img);? >

Use this script to generate the CAPTCHA in the page

  
      session_start ();? >
  <htmlxmlns="http://www.w3.org/1999/xhtml"xml: Lang="en"lang="en"><head>< Meta http-equiv= "content-type"Content="text/html; Charset=utf-8 " /><title>Guitar wars-add Your high score 
      title< /c24>>
  <linkrel="stylesheet"type="text/css"href ="Style.css" /> 
     Head
 ><body><h2>Guitar Wars-add Your High Score 
     H2
 >
    require_once(' appvars.php ');require_once(' connectvars.php ');if(isset($_post[' Submit '])) {//Connect to the database$dbc= Mysqli_connect (Db_host, Db_user, Db_password, db_name);//Grab The score data from the POST$name= Mysqli_real_escape_string ($dbc, Trim ($_post[' name ']));$score= Mysqli_real_escape_string ($dbc, Trim ($_post[' score ']));$screenshot= Mysqli_real_escape_string ($dbc, Trim ($_files[' screenshot '][' name ']));$screenshot _type=$_files[' screenshot '][' type '];$screenshot _size=$_files[' screenshot '][' Size '];//Check The CAPTCHA pass-phrase for verification$user _pass_phrase= SHA ($_post[' Verify ']);if($_session[' Pass_phrase '] ==$user _pass_phrase) {if(!Empty($name) && Is_numeric ($score) &&!Empty($screenshot)) {if((($screenshot _type==' Image/gif ') || ($screenshot _type==' Image/jpeg ') || ($screenshot _type==' Image/pjpeg ') || ($screenshot _type==' Image/png ')) && ($screenshot _size>0) && ($screenshot _size<= gw_maxfilesize)) {if($_files[' screenshot '][' ERROR '] ==0) {//Move the file to the target upload folder$target= Gw_uploadpath.$screenshot;if(Move_uploaded_file ($_files[' screenshot '][' Tmp_name '],$target)) {//Write The data to the database$query="INSERT into Guitarwars (date, name, score, screenshot) VALUES (now (), ' $name ', ' $score ', ' $screenshot ')"; Mysqli_query ($dbc,$query);//Confirm success with the userEcho'

Thanks for adding your new high score! It is reviewed and added to the high score list as soon as possible.

'
;Echo'

Name: ' . $name . '
'
; Echo 'score: ' . $score . '
'
; Echo ' . Gw_uploadpath. $screenshot . ' "alt=" PHP drawing function generates a picture verification code "/>

'
;Echo'

<< back to high scores

'
;//Clear the score data to clear the form$name="";$score="";$screenshot=""; Mysqli_close ($dbc); }Else{Echo'

Sorry, there is a problem uploading your screen shot image.

'
; } } }Else{Echo'

The screen shot must is a GIF, JPEG, or PNG image file no greater than '. (Gw_maxfilesize/ 1024x768). ' KB in size.

'
; }//Try to delete the temporary screen shot image file@unlink ($_files[' screenshot '][' Tmp_name ']); }Else{Echo'

Please enter any of the information to add your high score.

'
; } }Else{Echo'

Please enter the verification pass-phrase exactly as shown.

'
; } }?>
<hr /><formenctype= "multipart/form-data"method="POST" Action=" "><inputtype="hidden"name="max_file_size" Value=" " /><labelfor ="name">Name: Label >
<inputtype="text"id="Name"name=" name" Value=" " /><br /><labelfor ="Score">Score: Label ><inputtype="text"id="Score"name= "Score" Value=" " /><br /><labelfor ="screenshot">Screen shot: Label ><inputtype="file"id="screenshot"name=" Screenshot " /><br /><labelfor ="Verify">Verification: Label ><inputtype="text"id="Verify"name= "Verify" Value="Enter the pass-phrase." /><imgsrc="captcha.php"alt="Verification pass-phrase" /><hr /><inputtype="Submit"value="Add"name= "Submit" /> Form > Body > HTML >

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP drawing function to generate a picture verification code, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.