PHP Generate image Verification Code _php tutorial

Source: Internet
Author: User

PHP Generate Image Verification code


Verification codes are important in Web applications and are often used to prevent users from maliciously submitting forms, such as malicious registration and login, malicious forum flooding, and so on. This article will explain the use of PHP to generate common verification code

Let's take a look at the general effect.

So the next step is to put the code directly.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

$image = Imagecreatetruecolor (100, 30); Create a canvas

$imagecolor = Imagecolorallocate ($image, 255, 255, 255); Background color

Imagefill ($image, 0, 0, $imagecolor); Fill background color

for ($i =0; $i <4; $i + +) {//Loop 4-digit

$fontsize = 6;

$fontcolor = Imagecolorallocate ($image, rand (0, $), rand (0, $), rand (0, 200));

$fontcontent = rand (0, 9);

$x = $i *100/4 + rand (5, 15);

$y = rand (5, 10);

Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

}

for ($i =0; $i <200; $i + +) {//loop Add interference point

$pointcolor = Imagecolorallocate ($image, Rand (), Rand (50, 200));

$x = rand (1, 99);

$y = rand (1, 29);

Imagesetpixel ($image, $x, $y, $pointcolor);

}

for ($i =0; $i <3; $i + +) {//loop Add interference line

$linecolor = Imagecolorallocate ($image, rand (+), rand (100, 250));

$x 1 = rand (1, 25);

$x 2 = rand (50, 75);

$y 1 = rand (1, 15);

$y 2 = rand (15, 25);

Imageline ($image, $x 1, $y 1, $x 2, $y 2, $linecolor);

}

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

Imagepng ($image);

Imagedestroy ($image);

?>

And then we'll share one. Can generate Chinese code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

1.qi Enable GD Library GD library provides a series of APIs for working with images, using the GD library to process pictures, or to create images.

The GD library on the site is often used to generate thumbnails or to add watermarks to images or generate reports on site data.

Session_Start ();

The GBK encoded string into the UTF-8 string, the first argument is written GBK because the php file stored in the host encoding is GBK encoded

UTF-8 encoding Browser Universal support, strong versatility, this translates into UTF-8

$str = Iconv ("GBK", "Utf-8", "The mortal life of the Green Mountains and monuments open heart will clouds unroll happiness will always accompany you");

if (!is_string ($str) | |!mb_check_encoding ($STR, "Utf-8"))

{

Exit ("Not a string or not a utf-8");

}

$zhongwenku _size;

To get the length of a string by UTF-8 encoding

$zhongwenku _size = Mb_strlen ($str, "UTF-8");

Import the above characters into the array

$zhongwenku = Array ();

for ($i =0; $i < $zhongwenku _size; $i + +)

{

$zhongwenku [$i] = Mb_substr ($str, $i, 1, "UTF-8");

}

$result = "";

Four characters to write on a picture

for ($i =0; $i <4; $i + +)

{

Switch (rand (0, 1))

{

Case 0:

$result. = $zhongwenku [rand (0, $zhongwenku _size-1)];

Break

Case 1:

$result. =dechex (rand (0,15));

Break

}

}

$_session["Check"] = $result;

Create a true Color picture width 100, Height 30

$img = Imagecreatetruecolor (100, 30);

Assigning background colors

$BG = imagecolorallocate ($img, 0, 0, 0);

Assigning text colors

$te = Imagecolorallocate ($img, 255,255,255);

Write a string on a picture

Imagestring ($img, Rand (3,8), Rand (1,70), Rand (1,10), $result, $te);

A special font can be written on a picture based on the loaded font

Imagettftext ($IMG, Max, Rand (2, 9), $te, "Msyh." TTF ", $result);

$_session["Check"] = $result;

for ($i =0; $i <3; $i + +)

{

$t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));

Draw Line

Imageline ($img, 0, rand (0,), rand (70,100), rand (0,), $te);

}

$t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));

Add noise to a picture

for ($i =0; $i <200; $i + +)

{

Imagesetpixel ($img, rand (1, +), rand (1,), $t);

}

Send HTTP header information specifies that the JPEG in image is sent this time

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

Output JPEG image to browser

Imagejpeg ($IMG);

?>

One more example.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

Session_Start ();

function Random ($len) {

$srcstr = "1A2S3D4F5G6HJ8K9QWERTYUPZXCVBNM";

Mt_srand ();

$strs = "";

for ($i = 0; $i < $len; $i + +) {

$strs. = $srcstr [Mt_rand (0, 30)];

}

return $strs;

}

Randomly generated string

$STR = random (4);

Verify the width of the code picture

$width = 50;

Verify the height of the code picture

$height = 25;

Declare the picture format of the layer you want to create

@ Header ("Content-type:image/png");

Create a layer

$im = Imagecreate ($width, $height);

Background color

$back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF);

Blur Point Color

$pix = Imagecolorallocate ($im, 187, 230, 247);

Font Color

$font = Imagecolorallocate ($im, 41, 163, 238);

The point of painting blur function

Mt_srand ();

for ($i = 0; $i < $i + +) {

Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix);

}

Output character

Imagestring ($im, 5, 7, 5, $STR, $font);

Output Rectangle

Imagerectangle ($im, 0, 0, $width-1, $height-1, $font);

Output picture

Imagepng ($im);

Imagedestroy ($im);

$STR = MD5 ($STR);

Select a cookie

Setcookie ("Verification", $str, Time () + 7200, "/");

Select Session

$_session["verification"] = $STR;

?>

The next thing you can do is call it in the page:

?

1

If you want to achieve the "can't see" effect, add the following JS to the page

?

1

2

3

function changing () {

document.getElementById (' Checkpic '). src= "/images/checkcode.php?" +math.random ();

}

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1013643.html www.bkjia.com true http://www.bkjia.com/PHPjc/1013643.html techarticle PHP generated Image verification Code verification code is very important in Web applications, and is often used to prevent users from maliciously submitting forms, such as malicious registration and login, forum malicious irrigation and so on. This article will be explained by examples ...

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