PHP-Made Simple CAPTCHA identification code _php instance

Source: Internet
Author: User
Always wanted to write this, after a long time today interest came to simply record.

Verification Code

Fully automatic distinguish between computer and human public Turing Test (English: Completely automated publicly Turing test to tell Computers and humans Apart, abbreviated as CAPTCHA), commonly known as CAPTCHA, is a public automatic program that distinguishes users from computers and people. In the CAPTCHA test, a computer that acts as a server automatically generates an issue that is answered by the user. This problem can be generated and judged by a computer, but only humans can answer it. Since the computer is unable to answer the CAPTCHA question, the user who answers the question can be considered human.
Encyclopedia Introduction

The simple point is that randomly generated characters, output on a picture [here does not consider other forms of drag/SMS verification code, etc.].

Common types

Ideas

This article is only for demonstration purposes, so take the first picture verification code as an example.

Each point on the picture has its RGB value, can be obtained by the color picker, the naked eye can see that the figure verification code is a pure digital solid background

It is shown by the color picker that the RGB value of the verification Code background color is (212,214,204)

Realize

Let's use the PHP imagecolorsforindex function to get the RGB values of all the dots in the image:

$url = ' http://210.32.33.91:8080/reader/captcha.php '; $im = Imagecreatefromgif ($url); Imagegif ($im, ' 1.gif '); $  Rgbarray = Array (); $res = $im; $size = getimagesize ($url); $wid = $size [' 0 ']; $hid = $size [' 1 '];for ($i = 0; $i < $hid; + +) $i) {for ($j = 0; $j < $wid; + + $j) {  $rgb = Imagecolorat ($res, $j, $i);  $rgbArray [$i] [$j] = Imagecolorsforindex ($res, $RGB); }}

The results are as follows:

You may want to ask what is the use of this? Let's show the data in a different way, for the background color output , verify the Code area output , and then look at:

for ($i = 0; $i < $hid; $i + +) {for ($j = 0; $j < $wid; $j + +) {    if ($rgbArray [$i] [$j] [' red '] = = 212) {   ech O '-';  } else {   echo ';  }} Echo '
";}

Effect:

This is not very clear.

But you may still have questions, though you can see it, but how do you know how much it is?

Let's analyze the following:


Each verification code directly spaced 4 lattice, left and right spacing 6/10 lattice, up and down spacing 16/10 lattice.

Let's get rid of these disturbing points, and we can see more clearly:



Is it clear? Maybe someone would ask, how do you tell so much how to figure out what the numbers are on the picture?

Well, next to my train of thought, we'll just change to 0 and 1, and these numbers shape is fixed, so that you can get 0-9 each word of each region 8*10 are composed of 0 and 1,

Let's go through each of the 8 shards, remove the 4 spacing, and cycle through the 0-9 01 combined values:

 $dic = Array (' 00011000001111000110011011000011110000111100001111000011011001100011110000011000 ' = 0, ' 00011000001110000111100000011000000110000001100000011000000110000001100001111110 ' = 1, ' 00111100011001101100001100000011000001100000110000011000001100000110000011111111 ' = 2, ' 01111100110001100000001100000110000111000000011000000011000000111100011001111100 ' = 3, ' 00000110000011100001111000110110011001101100011011111111000001100000011000000110 ' = 4, ' 11111110110000001100000011011100111001100000001100000011110000110110011000111100 ' = 5, ' 00111100011001101100001011000000110111001110011011000011110000110110011000111100 ' = 6, ' 11111111000000110000001100000110000011000001100000110000011000001100000011000000 ' = 7, ' 00111100011001101100001101100110001111000110011011000011110000110110011000111100 ' = 8, ' 00111100011001101100001111000011011001110011101100000011010000110110011000111100 ' = 9); 

The 10 after the combination of the group, each resolution of the image RGB to the corresponding array value to get the verification code value. Below is a demonstration of:

Finally, for accuracy, take 100 loops to see:

Haha, accuracy rate 100%

Written in the last

The purpose of this article is to allow web developers to be aware of security when generating verification codes and not for illegal purposes.

The code is already on GitHub:

  • 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.