PHP generates CAPTCHA pictures from getting started and proficient in tutorials

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags array code content function graphics image name php

In PHP to generate a CAPTCHA image is quite simple, because in PHP for us to provide a graphics Gd.dll library, to enable the GD graphics library we just php.ini in the PHP-GD front;

Method One


$authnum = ';
$ychar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
$list =explode (",", $ychar);//Split function
For ($i =0 $i <4; $i + +) {
$randnum =rand (0,35);
$authnum. = $list [$randnum];//output as an array

Method Two:


Private Function Createcheckcode ()
{
for (i=0;i<this->codenum;i++)
{
Number = rand (0,2);
Switch (number)
{
Case 0:rand_number = rand (48,57); break;//Digital
Case 1:rand_number = rand (65,90); break;//uppercase letters
Case 2:rand_number = rand (97,122); break;//lowercase Letters
}
$ASC = sprintf ("%c", Rand_number);
$ASC _number = ASC_NUMBER.ASC;
}
return asc_number;
}

Method Three:


Srand (Microtime () *100000);
$string = "abcdefghigklmnopqrstuvwxyz123456789";
For ($i =0 $i <4; $i + +)
{
$new _number.= $string [Rand (0,strlen ($string)-1)];//immediately produces an array
}

Method Four:


For ($i =0 $i <4; $i + +)
{
$rand. =dechex (rand (1,15));//convert decimal to hexadecimal
}



Randomly generated numbers, letters code:


<?php
che.php
Session_Start ();
For ($i =0 $i <4; $i + +)
{
$rand. =dechex (rand (1,15));
}
$_session[' Check_num ']= $rand;
$image =imagecreatetruecolor (50,30);
$BG =imagecolorallocate ($im, 0,0,0);//The first time you use a palette, the background color
$te =imagecolorallocate ($im, 255,255,255);
Imagestring ($image, 6,rand (0,20), Rand (0,2), $rand, $te);
Ob_clean ()//php the image "http://localhost/**.php" in the Web page because the validation code is to be generated cannot be displayed because of its own error
Header ("Content-type:image/jpeg"); Imagejpeg ($image);
?>

Draw the interference line code for the picture:


For ($i =0 $i <8; $i + +)//Draw more than one line
{
$CG =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//produce a random color
Imageline ($im, Rand (10,40), 0,rand (10,40), $CG);
}

To draw a disturbing point to the picture code:


For ($i =0 $i <80; $i + +)//Draw multiple points
{
Imagesetpixel ($im, Rand (0,40), Rand (0,20), $CG);
}

Write text to the picture code:


$str =array (' i ', ' I ', ' pro ', ' Pro ');//store displayed Kanji
For ($i =0 $i <4; $i + +)
{
$sss. = $str [Rand (0,3)];//randomly displays Chinese characters
}

$str =iconv ("gb2312", "Utf-8", $str); Character transformation, I don't seem to need
Imagettftext ($im, 10,0,rand (5,60), Rand (5,60), $te, "Simhei.ttf", $sss);

Finally, we share a complete example

 


/**


* Generate Verification code picture


 *


* @param String $word The variable name of the validation code in session


 */


function Valicode ($word = ' Randcode ') {


Header ("Content-type:image/gif");


$border = 0; Do you want to have a border 1:0 Don't


$how = 4; Verify code digits


$w = $how *15; Picture width


$h = 18; Picture Height


$fontsize = 10; Font size


$alpha = "ABCDEFGHIJKMNPQRSTUVWXYZ"; CAPTCHA Content 1: Letter


$number = "23456789"; Authenticode Content 2: Digital


$randcode = ""; Authentication Code String Initialization


Srand (Double) microtime () *1000000); Initialization of random number seed


$im = imagecreate ($w, $h); Create a validation picture


 /*


* Draw Basic frame


 */


$bgcolor = imagecolorallocate ($im, 255, 255, 255); Set Background color


Imagefill ($im, 0, 0, $bgcolor); Fill background Color


if ($border)


 {


$black = imagecolorallocate ($im, 0, 0, 0); Set Border color


Imagerectangle ($im, 0, 0, $w-1, $h-1, $black);//Draw Border


 }


 


 /*


* generates random characters on a bit


 */


for ($i =0; $i &lt; $how; $i + +)


 {


$alpha _or_number = Mt_rand (0, 1); Letters or numbers


$str = $alpha _or_number? $alpha: $number;


$which = Mt_rand (0, strlen ($str)-1); Which character to take


$code = substr ($str, $which, 1); Take character


$j = $i? 4: $j +15; Draw character Position


$color 3 = imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100)); Character immediately color


Imagechar ($im, $fontsize, $j, 3, $code, $color 3); Painting character


$randcode. = $code; Step-by-step addition of Authenticode string


 }


 


 /*


* Remove Comments If you need to add interference


 *


* the following for () loop for drawing background interference line code


 */


/* +-------------------------------Draw background interference line start--------------------------------------------+ */


for ($i =0 $i &lt;5; $i + +)//Draw background interference line


 {


$color 1 = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Interference Line Color


Imagearc ($im, Mt_rand ( -5, $w), Mt_rand ( -5, $h), Mt_rand (20,300), Mt_rand (20,200), $color 1); Interference Line


 }


/* +-------------------------------Draw background interference line end--------------------------------------+ */


 


 /*


* Remove Comments If you need to add interference


 *


* The following for () loop for drawing background interference point code


 */


/* +--------------------------------Draw background interference point start------------------------------------------+ */


 


For ($i =0 $i &lt; $how *40 $i + +)//Draw background interference points


 {


$color 2 = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Interference point Color


Imagesetpixel ($im, Mt_rand (0, $w), Mt_rand (0, $h), $color 2); Jamming Point


 }


 


/* +--------------------------------Draw background interference point end------------------------------------------+ */


 


///write the verification code string to the session to verify that the verification code is correct when submitting the login information for example: $_post[' randcode '] = $_session[' Randcode ']


$_session[$word] = $randcode;


/* Drawing End/


imagegif ($im);


Imagedestroy ($im);


/* Drawing End/


}

Invoking the method is also very easy to save the img.php file with the above instance, and then on the page you want to call
The HTML page looks like this


<script language= "JavaScript" >
function Refresh_code ()
{
Form1.imgcode.src= "Verifycode.php?a=" +math.random ();
}
</script>

<form id= "Form1" Name= "Form1" method= "Post" action= "checkcode.php" >
<label for= "Code" > Verification Code:</label>
<input type= "text" name= "code" id= "TextField"/>
<img id= "Imgcode" src= "verifycode.php" alt= "Authentication Code"/>
<a href= "Javascript:refresh_code ()" > Can't see clearly? Another </a>
<input type= "Submit" name= "button" id= "button" value= "submitted"/>
</form>

This will enable verification code invocation.

Add a commit verify that the code is correct


<?php
Session_Start ();
if ((Strtoupper ($_post["code")) = = Strtoupper (($_session["Verifycode"))) {
Print ("Verify code is correct,");
}else{
Print ("Validation code error,");
}

?>

This is all we have to do from generating the captcha picture and using the entire process, from the beginning of PHP to the proficiency of the verification code.

Related Article

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.