Php verification code generation image tutorials

Source: Internet
Author: User
Tags border color comments getting started with php html page php file rand strlen uppercase letter

Method 1

The code is as follows: Copy code

$ Authnum = '';
$ Ychar = ", 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); // delimiter function
For ($ I = 0; $ I <4; $ I ++ ){
$ Randnum = rand (0, 35 );
$ Authnum. = $ list [$ randnum]; // output as an array

Method 2:
 

The code is as follows: Copy code
Private function createCheckCode ()
{
For (I = 0; I <this-> codeNum; I ++)
{
Number = rand (0, 2 );
Switch (number)
{
Case 0: rand_number = rand (48, 57); break; // number
Case 1: rand_number = rand (); break; // uppercase letter
Case 2: rand_number = rand (97,122); break; // lowercase letter
}
$ Asc = sprintf ("% c", rand_number );
$ Asc_number = asc_number.asc;
}
Return asc_number;
}

Method 3:
 

The code is as follows: Copy code
Srand (microtime () * 100000); // equivalent to a timer
$ String = "abcdefghigklmnopqrstuvwxyz123456789 ";
For ($ I = 0; $ I <4; $ I ++)
{
$ New_number. = $ string [rand (0, strlen ($ string)-1)]; // An array is generated immediately.
}

Method 4:
 

The code is as follows: Copy code

For ($ I = 0; $ I <4; $ I ++)
{
$ Rand. = dechex (rand (); // Convert decimal to hexadecimal
}


 

Code for randomly generating numbers and letters:

 

The code is as follows: Copy 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); // specifies the background color when the palette is used for the first time.
$ Te = imagecolorallocate ($ im, 255,255,255 );
Imagestring ($ image, 6, rand (0, 20), rand (0, 2), $ rand, $ te );
Ob_clean (); // The Image "http: // localhost/**. PHP" is displayed on the php webpage because the verification code is generated.
Header ("Content-type: image/jpeg"); imagejpeg ($ image );
?>

Draw the interference line code for the image:
 

The code is as follows: Copy code
For ($ I = 0; $ I <8; $ I ++) // draw multiple lines
{
$ Cg = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); // Generate random colors
Imageline ($ im, rand (10, 40), 0, rand (10, 40), 20, $ cg );
}

Draw the interference point code for the image:
 

The code is as follows: Copy code
For ($ I = 0; $ I <80; $ I ++) // draw multiple points
{
Imagesetpixel ($ im, rand (0, 40), rand (0, 20), $ cg );
}

Write text into the image code:
 

The code is as follows: Copy code

$ Str = array ('my', 'my', 'login', 'login'); // store the displayed Chinese characters
For ($ I = 0; $ I <4; $ I ++)
{
$ Sss. = $ str [rand ()]; // randomly display Chinese characters
}

// $ Str = iconv ("gb2312", "UTF-8", $ str); // Chinese character encoding conversion, which does not seem to require me
Imagettftext ($ im, 10, 0, rand (5, 60), rand (5, 60), $ te, "simhei. ttf", $ sss );//

Finally, we will share a complete instance.

 

The code is as follows: Copy code
/**
* Generate a verification code Image
 *
* @ Param String $ name of the variable in the session of the word verification code
*/
Function valiCode ($ word = 'randcode '){
Header ("Content-type: image/gif ");
$ Border = 0; // whether the border is required; 1: 0: not required
$ How = 4; // Number of digits of the verification code
$ W = $ how * 15; // image width
$ H = 18; // Image height
$ Fontsize = 10; // font size
$ Alpha = "abcdefghijkmnpqrstuvwxyz"; // verification code content 1: letter
$ Number = "23456789"; // verification code content 2: number
$ Randcode = ""; // verification code string initialization
Srand (double) microtime () * 1000000); // initialize the random number seed
$ Im = ImageCreate ($ w, $ h); // Create a verification image
/*
* Draw the basic framework
*/
$ Bgcolor = ImageColorAllocate ($ im, 255,255,255); // you can specify the background color.
ImageFill ($ im, 0, 0, $ bgcolor); // fill the background color
If ($ border)
 {
$ Black = ImageColorAllocate ($ im, 0, 0, 0); // you can specify the border color.
ImageRectangle ($ im, 0, 0, $ W-1, $ H-1, $ black); // draw a border
 }
 
/*
* Generate random characters by bit
*/
For ($ I = 0; $ I <$ 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); // specify the character
$ Code = substr ($ str, $ which, 1); // Obtain the character
$ J =! $ I? 4: $ j + 15; // specifies the position of the painted character.
$ Color3 = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100); // character color
ImageChar ($ im, $ fontsize, $ j, 3, $ code, $ color3); // draw characters
$ Randcode. = $ code; // add a verification code string to a bit by bit
 }
 
/*
* Remove comments if you need to add interference.
 *
* The following for () loop is the background interference line code.
*/
/* + ------------------------------- Start of the background interference line ---------------------------------------------- + */
For ($ I = 0; $ I <5; $ I ++) // draws the background interference line
 {
$ Color1 = 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), 55, 44, $ color1); // interference line
 }
/* + ------------------------------- End of the painted background interference line ------------------------------------ + */
 
/*
* Remove comments if you need to add interference.
 *
* The following for () loop is the background interference point code.
*/
/* + -------------------------------- Start of the painted background interference point ------------------------------------------ + */
 
For ($ I = 0; $ I <$ how * 40; $ I ++) // draws background interference points
 {
$ Color2 = 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), $ color2); // interference point
 }
 
/* + -------------------------------- The background interference point is painted. ---------------------------------------- + */
 
// Write the verification code string to the session to check whether the verification code is correct when the logon information is submitted. For example, $ _ POST ['randcode'] = $ _ SESSION ['randcode']
$ _ SESSION [$ word] = $ randcode;
/* Drawing ends */
Imagegif ($ im );
ImageDestroy ($ im );
/* Drawing ends */
}

The call method is also very easy to save the img. Php file of the above instance, and then on the page to be called
The html page is as follows:

The code is as follows: Copy code

<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"/>

<A href = "javascript: refresh_code ()"> Why not? Change one </a>
<Input type = "submit" name = "button" id = "button" value = "submit"/>
</Form>

You can call the verification code.

Check whether the verification code is correct.

The code is as follows: Copy code

<? Php
Session_start ();
If (strtoupper ($ _ POST ["code"]) = strtoupper ($ _ SESSION ["VerifyCode"]) {
Print ("The verification code is correct ,");
} Else {
Print ("verification code error ,");
  }

?>

This completes the entire process from generating the verification code image and using it. It can be counted from getting started with php to mastering the 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.