PHP dynamically generated verification code

Source: Internet
Author: User
Tags php script

...... Robots search the Web for input forms that allow them to insert ads, and nothing in the virtual world can stop them from misbehaving. These robots are extremely efficient and do not care about the original use of the form being attacked. Their only goal is to use their junk ads to cover your content, brutally seeking advertising revenue for their owners.

To verify that a form is facing a real person, this test is called CAPTCHA (fully automated public Turing test). The most effective method now is to generate a random pass phrase that requires user input, in order to prevent robotic cracking systems that support OCR (optical character recognition), the passphrase must be deformed, or partially blurred with random lines and dots.

/*generate a sequence of verification codes*/Define("Captcha_numchars", 6);//Verification Code Length$pass _phrase= "";//Verification Code Content for($i= 0;$i< Captcha_numchars;$i++) {    //randomly generated letters added to verification code    $pass _phrase.=CHR(Rand(97, 122));}/*generate a CAPTCHA image*/Define("Captcha_width", 300);//Verification Code WidthDefine("Captcha_height", 100);//Verify Code Height//create blank canvas$img= Imagecreatetruecolor (Captcha_width,captcha_height);//Set Theme Colors$BG _color= Imagecolorallocate ($img, 225, 225, 225);//White background$text _color= Imagecolorallocate ($img, 0, 0, 0);//Black Font$graphic _color= Imagecolorallocate ($img, 64, 64, 64);//Gray image//fill backgroundImagefilledrectangle ($img, 0, 0, captcha_width, Captcha_height,$BG _color);//draw a random line for($i= 0;$i< 10;$i++) {Imageline ($img, 0,Rand()% Captcha_height, Captcha_width,Rand()% Captcha_height,$graphic _color);}//Draw Random points for($i= 0;$i< 20;$i++) {Imagefilledellipse ($img,Rand()% Captcha_width,Rand()% Captcha_height, 10, 10,$graphic _color);}//Draw a verification codeImagettftext ($img, 0, Captcha_height-20,$text _color, "SketchFlow Print.ttf",$pass _phrase);//output as PNG imageHeader("Content-type:image/png"); Imagepng ($img);//undoing images from memoryImagedestroy ($img);

Dynamically generated images using PHP are not stored in a file, in fact, the imagepng () function generates a binary PNG image in the server's memory and then transfers it directly to the browser via a header.

This is why the PHP script can be placed directly in the SRC attribute of the tag:

PHP dynamically generated verification code

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.