search
HomeBackend DevelopmentPHP TutorialGenerate image verification code with php_PHP tutorial
Generate image verification code with php_PHP tutorialJul 13, 2016 am 09:51 AM
phpwebpictureexistapplicationgenerateusercodeimportantpreventverify

php generates image verification code

Verification code is very important in WEB applications. It is usually used to prevent users from maliciously submitting forms, such as malicious registration and login, malicious forum flooding, etc. This article will explain how to use PHP to generate common verification codes through examples

Let’s take a look at the rough effect first

Then let’s just post the code next

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

$image = imagecreatetruecolor(100, 30); //创建画布

$imagecolor = imagecolorallocate($image, 255, 255, 255); //背景色

imagefill($image, 0, 0, $imagecolor); //填充背景色

for($i=0;$i

$fontsize = 6;

$fontcolor = imagecolorallocate($image, rand(0, 200), rand(0, 200), 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

$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));

$x = rand(1, 99);

$y = rand(1, 29);

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

}

for($i=0;$i

$linecolor = imagecolorallocate($image, rand(100, 250), rand(100, 250), rand(100, 250));

$x1 = rand(1, 25);

$x2 = rand(50, 75);

$y1 = rand(1, 15);

$y2 = rand(15, 25);

imageline($image, $x1, $y1, $x2, $y2, $linecolor);

}

header("content-type:image/png");

imagepng($image);

imagedestroy($image);

?>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
$image = imagecreatetruecolor(100, 30); //Create canvas $imagecolor = imagecolorallocate($image, 255, 255, 255); //Background color imagefill($image, 0, 0, $imagecolor); //Fill the background color for($i=0;$i $fontsize = 6; $fontcolor = imagecolorallocate($image, rand(0, 200), rand(0, 200), 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 $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); $x = rand(1, 99); $y = rand(1, 29); imagesetpixel($image, $x, $y, $pointcolor); } for($i=0;$i $linecolor = imagecolorallocate($image, rand(100, 250), rand(100, 250), rand(100, 250)); $x1 = rand(1, 25); $x2 = rand(50, 75); $y1 = rand(1, 15); $y2 = rand(15, 25); imageline($image, $x1, $y1, $x2, $y2, $linecolor); } header("content-type:image/png"); imagepng($image); imagedestroy($image); ?>

Let me share with you another method that can generate Chinese verification code

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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 the gd library. The GD library provides a series of APIs for processing images. You can use the GD library to process images or generate images.

// On websites, the GD library is usually used to generate thumbnails or to add watermarks to images or to generate reports on website data.

session_start();

// Convert the GBK encoded string into a UTF-8 string. The reason why the first parameter is written GBK is because the encoding of this php file stored in the host is GBK encoding

// UTF-8 encoding is generally supported by browsers and has strong versatility. Let’s convert it to UTF-8 here

$str = iconv("GBK", "utf-8", "All living things, green waters, green mountains, scenic spots and historic sites, open your mind and you will see clouds and clouds, and happiness will always be with you");

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

{

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

}

$zhongwenku_size;

// Get the length of the string in UTF-8 encoding

$zhongwenku_size = mb_strlen($str,"UTF-8");

// Import the above characters into the array

$zhongwenku = array();

for( $i=0; $i

{

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

}

$result = "";

//Four characters to be written on the picture

for($i=0; $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 with a width of 100 and a height of 30

$img = imagecreatetruecolor(100, 30);

//Assign background color

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

//Assign text color

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

//Write string on the image

//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);

//You can write special fonts on the picture according to the loaded font

imagettftext($img, 13, rand(2, 9), 20 ,20, $te, "MSYH.TTF",$result);

$_SESSION["check"] = $result;

for($i=0; $i

{

// $t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255));

// Draw lines

imageline($img, 0, rand(0, 20), rand(70,100), rand(0, 20), $te);

}

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

// Add noise to the image

for($i=0; $i

{

imagesetpixel($img, rand(1, 100), rand(1, 30), $t);

}

//Send http header information. Specify that the jpeg in the image is sent this time

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

// Output jpeg images to the browser

imagejpeg($img);

?>

Let’s give another example

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

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

}

return $strs;

}

 

//随机生成的字符串

$str = random(4);

 

//验证码图片的宽度

$width = 50;

 

//验证码图片的高度

$height = 25;

 

//声明需要创建的图层的图片格式

@ header("Content-Type:image/png");

 

//创建一个图层

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

 

//背景色

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

 

//模糊点颜色

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

 

//字体色

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

 

//绘模糊作用的点

mt_srand();

for ($i = 0; $i

imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);

}

 

//输出字符

imagestring($im, 5, 7, 5, $str, $font);

 

//输出矩形

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

 

//输出图片

imagepng($im);

 

imagedestroy($im);

 

$str = md5($str);

 

//选择 cookie

//SetCookie("verification", $str, time() 7200, "/");

 

//选择 Session

$_SESSION["verification"] = $str;

?>

1

2

3

1

Generate image verification code with php_PHP tutorial

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 $strs .= $srcstr[mt_rand(0, 30)]; } return $strs; } //Randomly generated string $str = random(4); //The width of the verification code image $width = 50; //The height of the verification code image $height = 25; //Declare the image format of the layer to be created @ header("Content-Type:image/png"); //Create a layer $im = imagecreate($width, $height); //Background color $back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); //Blurred point color $pix = imagecolorallocate($im, 187, 230, 247); //Font color $font = imagecolorallocate($im, 41, 163, 238); //Draw the blur effect point mt_srand(); for ($i = 0; $i imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix); } //Output characters 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 cookies //SetCookie("verification", $str, time() 7200, "/"); //Select Session $_SESSION["verification"] = $str; ?>
Then just call it on the page:  ?
1 Generate image verification code with php_PHP tutorial

If you want to achieve the "Can't see clearly? Change another" effect, add the following JS to the page

 ?

1

2

3

function changing(){

document.getElementById('checkpic'). Math.random();

}

1

2 3

document.getElementById('checkpic').src="/images/checkcode.php?" Math.random(); }
The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1013643.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/1013643.htmlTechArticlephp generated image verification code Verification code is very important in WEB applications. It is usually used to prevent users from maliciously submitting forms, such as Malicious registration and login, malicious forum spamming, etc. This article will explain through examples...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software