Example of implementing color-changing verification code in PHP

Source: Internet
Author: User

  Verification code presumably everyone has seen it, in this article for you to introduce how to implement the color of PHP code, interested friends can refer to the following

The code is as follows: <?php  header ("content-type:image/png,charset= ' Utf-8 '");  $im = Imagecreatetruecolor (400, 30);  //white   $white = imagecolorallocate ($im, 255, 255, 255); //red   $red = Imagecolorallocate ($im, 255, 0, 0; //Black   $black =imagecolorallocate ($im, 0, 0, 0); //green   $green =imagecolorallocate ($im, 0, 255, 0) ; //Blue   $blue =imagecolorallocate ($im, 0, 0, 255);  $color _arr=array ($green, $blue, $red);  $color = Array_rand ($color _arr);  $text = ' I'm so sick of this verification code ';  $textlen =iconv_strlen ($text, ' utf-8 '); Compute string length  // Randomly intercept two characters, color display   $p 1=rand (1, $textlen) -1;  while (($p 2=rand (1, $textlen)-1) = = $p 1);  $w 1=iconv_substr ($ Text, $p 1,1, ' utf-8 ');  $w 2=iconv_substr ($text, $p 1,1, ' utf-8 '); //font file (ps:t good PHP Q-buckle 峮: 276167802, Validation: CSL)   $font = ' Simkai.ttf ';  imagefilledrectangle ($im, 0, 0, 399, $white);  for ($i =0; $i < $textlen; $i + +)   {  if ($i = = $p 1| | $i = = $p 2)   {  Imagettftext ($im,0, 20* ($i-1) +20, $color _arr[$color], $font, Iconv_substr ($text, $i, 1, ' utf-8 ')); }  else  Imagettftext ($im, 0, 20* ($i-1) +20, $black, $font, Iconv_substr ($text, $i, 1, ' utf-8 ')); }  Imagepng ($im);  Imagedestroy ($im); ?>    Verify that the characters in the code are not the same color, allowing the user to enter a CAPTCHA for the specified color, which is better for security.    

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.