Generate Verification Code in PHP

Source: Internet
Author: User

Beginner web, is trying to write a blog, registration needs to use the verification code. I have learned the book head first in the code, changed the Chinese comments, put on

<?PHPSession_Start(); //defining the Important constants    Define(' Captcha_numchars ', 6); Define(' Captcha_width ', 100); Define(' Captcha_height ', 25); //generate a phrase    $pass _phrase="";  for($i= 0;$i<CAPTCHA_NUMCHARS;$i++)    {        $pass _phrase.=CHR(Mt_rand(97,122)); }    //stored in the session    $_session[' Pass_phrase ']=SHA1($pass _phrase); //Create an image    $img=imagecreatetruecolor (Captcha_width,captcha_height); //set white background black text gray pattern    $BG _color=imagecolorallocate ($img, 255,255,255); $text _color=imagecolorallocate ($img, 0,0,0); $graphic _color=imagecolorallocate ($img, 64,64,64); //Fill BackgroundImagefilledrectangle ($img, 0,0,captcha_width,captcha_height,$BG _color); //draw some random lines.     for($i= 0;$i<3;$i++) {Imageline ($img, 0,Rand()%captcha_height,captcha_width,Rand()%captcha_height,$graphic _color); }    //draw a few random points     for($i= 0;$i<50;$i++) {Imagesetpixel ($img,Rand()%captcha_width,Rand()%captcha_height,$graphic _color); }    //draw a phraseImagettftext ($img, 18,0,5,captcha_height-5,$text _color, "Courier New Bold.ttf",$pass _phrase); //creating PNG images    Header("Content-type:image/png"); Imagepng ($img); //Clear CacheImagedestroy ($img);?>

The basic idea is to use the GD library to create an image, and then randomly draw lines, dots, and characters. The use of the function can be found, here is no longer to repeat, to say a few questions I encountered.

1. After opening the above page, the browser display picture does not load correctly, Baidu provides a solution, is to add the Ob_clean ( ) function before the header () function, clear the cache, and then successfully opened.

2. Open after the image does not have a verification code, suspected Imagettftext () function of the problem, but did not solve, in the online search to find someone else about the code, the Imagettftext () function is replaced by imagestring (), So the show was successful.

Here again put another person's code, the effect is better than the book, posted in Baidu know on the question, should not say I tort it = =

<?PHPSession_Start();//Create random Code for($i= 0;$i<4;$i++){    $_nmsg.=Dechex(Mt_rand(0, 15));}//save it in the session.$_session[' Code '] =$_nmsg;//Long and high$_width= 75;$_height= 25;//Create an image$_img= Imagecreatetruecolor ($_width,$_height);$_white= Imagecolorallocate ($_img, 255, 255, 255); Imagefill ($_img, 0, 0,$_white);//Create a black border$_black= Imagecolorallocate ($_img, 100, 100, 100); Imagerectangle ($_img, 0, 0,$_width-1,$_height-1,$_black);//Random Dash Bar for($i= 0;$i<6;$i++) {$_rnd_color= Imagecolorallocate ($_img,Mt_rand(0,255),Mt_rand(0,255),Mt_rand(0,255)); Imageline ($_img,Mt_rand(0,75),Mt_rand(0,25),Mt_rand(0,75),Mt_rand(0,25),$_rnd_color);}//randomly hit snowflakes for($i= 1;$i<100;$i++) {imagestring ($_img, 1,Mt_rand(1,$_width),Mt_rand(1,$_height),"*",Imagecolorallocate ($_img,Mt_rand(200,255),Mt_rand(200,255),Mt_rand(200,255)));}//Output Verification Code for($i= 0;$i<strlen($_session[' Code ']);$i++) {imagestring ($_img,Mt_rand(3,5),$i*$_width/4+Mt_rand(1,10),Mt_rand(1,$_height/2),$_session[' Code '] [$i],Imagecolorallocate ($_img,Mt_rand(0,150),Mt_rand(0,100),Mt_rand(0,150)));}//Output ImageOb_clean(); Header(' Content-type:image/png '); Imagepng ($_img);//destroyedImagedestroy ($_img);?>

Generate Verification Code in PHP

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.