Let's talk about some of the best verification codes in PHP in the past few years.

Source: Internet
Author: User

Let's talk about some of the best verification codes in PHP in the past few years.

The verification code is now a basic knowledge point on the website. The existence of the verification code can prevent malicious password cracking, ticket flushing, and irrigation, effectively prevent brute force cracking on specific users.

Now let's take a look at some of the best verification codes in PHP.

Take the four-digit Verification Code as an example ).

At present, most websites still use static image verification codes, because it is simple and convenient to implement and does not require excellent skills. Of course, this is also the basis. The principle is to use the PHP drawing function to draw text into an image and return it to the page. Therefore, we have only three steps to solve the problem:

Of course, PHP does not enable the plotting function by default. Therefore, enable the plotting function in the configuration file of php. ini: search; extension = php_gd2.dll: remove the semicolon. (Remember to save it !)

According to the first point, you should first think of the digit verification code, because it is easy to randomly generate a few numbers, and then combine them, just like soy sauce!

1 $ validateCode = ''; 2 for ($ I = 0; $ I <4; $ I ++) {3 $ validateCode. = rand (0, 9); 4} 5 echo $ validateCode;View Code

Then think about it. The website Verification Code now has letters, but there are no letters in it. Why? Then I thought that it would be nice to make the above stuff into a hexadecimal system.

1 $ validateCode = ''; 2 for ($ I = 0; $ I <4; $ I ++) {3 $ validateCode. = dechex (rand (0, 16); 4} 5 echo $ validateCode;View Code

It seems to be a little clever, but think about it, there are only a few letters, but only a, B, c, d, e, f, what if we need all the letters? Haha, when you think about it, you just want to list all the letters and numbers in one string, and then use the random downlink form.
Obtain the corresponding value randomly:

1 $ validateCode = ''; 2 $ str = 'hangzhou'; 3 for ($ I = 0; $ I <4; $ I ++) {4 $ validateCode. = $ str [rand (0, strlen ($ str)]; 5} 6 echo $ validateCode;View Code

Haha, I finally got like a random number in the verification code, but I can see the following method, which is always in seconds:

echo substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'), 0, 4);

The str_shuffle method breaks the string content. substr ($ str, 0, 4) gets the first four digits. There is no problem! However, the content in such a verification code will not be repeated.

 

The above verification code is generated, and the verification code will be drawn using PHP.

1 $ validateCode = substr (str_shuffle ('hangzhou'), 0, 4); 2 3 // create an image with a width of 110px and a height of 25px 4 $ img = imagecreatetruecolor (110, 25); 5 // set the background color of the image to black. The color values correspond to RGB 6 $ bgcolor = imagecolorallocate ($ img, 0, 0, 0 ); 7 8 // create white 9 $ white = imagecolorallocate ($ img, 255,255,255); 10 11 // Add text to the image. The second parameter is the text size, which can only be 1 ~ 5. The third parameter is the x coordinate, the third parameter is the y coordinate, the third parameter is the text content, and the third parameter is the text color 12 imagestring ($ img, rand (3, 5), rand (0, 80), rand (2, 10), $ validateCode, $ white); 13 14 // Add text in the image, the second parameter is the text size, which can only be 1 ~ 5. The third parameter is the x coordinate, the third parameter is the y coordinate, the third parameter is the text content, and the third parameter is the text color 15 imagestring ($ img, rand (3, 5), rand (0, 80), rand (2, 10), $ validateCode, $ white );View Code

  

Of course, in this case, the verification code is too easy to recognize, just black and white words. To increase the difficulty, we will add a few more lines to the Verification code.

1 $ validateCode = substr (str_shuffle ('hangzhou'), 0, 4); 2 3 // create an image with a width of 110px and a height of 25px 4 $ img = imagecreatetruecolor (110, 25); 5 // set the background color of the image to black. The color values correspond to RGB 6 $ bgcolor = imagecolorallocate ($ img, 0, 0, 0 ); 7 // imagefill ($ img, 10, 10, $ bgcolor); 8 9 // create white 10 $ white = imagecolorallocate ($ img, 255,255,255 ); 11 12 for ($ I = 0; $ I <30; $ I ++) {13/ /Draw a line on the image. Among them, 2nd parameters represent the starting coordinate of the Line x, 3rd parameters represent the starting coordinate of the line y, and 4th parameters represent the ending coordinate of the Line x, the first parameter represents the ending coordinate of y, and the second parameter represents the color. Here a random color 14 imageline ($ img, rand (5th), rand (0, 50) is generated ), rand (0,110), rand (0, 50), imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ))); 15} 16 17 // Add text to the image. The second parameter is the text size, which can only be 1 ~ 5. The third parameter is the x coordinate, the third parameter is the y coordinate, the third parameter is the text content, and the third parameter is the text color 18 imagestring ($ img, rand (3, 5), rand (0, 80), rand (2, 10), $ validateCode, $ white ); 19 20 // return to the image 21 header ('content-type: image/png '); 22 imagepng ($ img );View Code

The result is like this. Of course, to implement the verification code, it is not enough to save the random number generated by the Verification code to the server session, $ _ SESSION ['validate'] = $ validateCode; then, compare the verification code entered on the client. This is the most basic code in PHP. You can change it to an arithmetic verification code based on your ideas, chinese verification code, or some other interesting verification code, to reduce its boring nature and achieve security.

 

Do you have any other verification codes to share with me?

 


Verification Code in PHP

That's because register_global = off in php. ini;
You can change it to on, but after php5, it is set to off by default,
It is not safe to get the value directly. If the user knows the name of your session
Can he modify your session through get or post?
Because the value is just the name!
Php5 generally uses super global variables!
$ _ GET
$ _ POST
$ _ COOKIE
$ _ SESSION

Another author's book seems a little old. session_register is no longer recommended in php5. Now, $ _ SESSION ['yzm'] = 100 can be assigned directly;

Note: I heard that php6 has come out!

Question about generating image verification codes in php

The cause is found: $ rand variable is not initialized, and a warning is reported when it is directly output. The solution is to add $ rand = ''before the for statement.
Session_start ();
$ Rand = '';
For ($ I = 0; $ I <4; $ I ++ ){
$ Rand. = dechex (rand (0, 15 ));
}
$ _ SISSION ['rand'] = $ rand;

$ Bg = imagecreatetruecolor (100,30 );
$ Im = imagecolorallocate ($ bg, 0, 0 );
$ Color = imagecolorallocate ($ bg, 255,255,255 );
Imagestring ($ bg, 5, 0, 0, $ rand, $ color );

Header ("Content-type: image/jpeg ");
Imagejpeg ($ bg );

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.