Maison > développement back-end > tutoriel php > php使用for循环生成4位验证码只生成了3位

php使用for循环生成4位验证码只生成了3位

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-06-06 20:14:43
original
1372 Les gens l'ont consulté

我是用php for循环生成验证码,经常出现有一位验证码没能生成的情况,希望了解问题的大神的帮忙看下代码,非常感谢!

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

<code><?php $image = imagecreatetruecolor(100,30);

$bgcolor = imagecolorallocate($image,255,255,255);

imagefill($image,0,0,$bgcolor);

 

// 生成干扰像素点

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

    $pixelcolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));

    imagesetpixel($image,rand(1,99),rand(1,29),$pixelcolor);

}

 

    // 生成干扰直线

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

    $linecolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));

    imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);

}

 

//生成验证码

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

    $fontsize = 6;

    $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));

    $data = 'abcdefghijkmnpqrstuvwxy3456789';

    $fontcontent = substr($data,rand(0,strlen($data)),1);

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

    $y = rand(5,10);

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

}

 

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

imagepng($image);

imagedestroy($image);

?>

</code>

Copier après la connexion
Copier après la connexion

php使用for循环生成4位验证码只生成了3位
上图为生成效果

回复内容:

我是用php for循环生成验证码,经常出现有一位验证码没能生成的情况,希望了解问题的大神的帮忙看下代码,非常感谢!

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

<code><?php $image = imagecreatetruecolor(100,30);

$bgcolor = imagecolorallocate($image,255,255,255);

imagefill($image,0,0,$bgcolor);

 

// 生成干扰像素点

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

    $pixelcolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));

    imagesetpixel($image,rand(1,99),rand(1,29),$pixelcolor);

}

 

    // 生成干扰直线

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

    $linecolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));

    imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);

}

 

//生成验证码

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

    $fontsize = 6;

    $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));

    $data = 'abcdefghijkmnpqrstuvwxy3456789';

    $fontcontent = substr($data,rand(0,strlen($data)),1);

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

    $y = rand(5,10);

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

}

 

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

imagepng($image);

imagedestroy($image);

?>

</code>

Copier après la connexion
Copier après la connexion

php使用for循环生成4位验证码只生成了3位
上图为生成效果

1个可能的原因

$fontcontent = substr($data,rand(0,strlen($data)),1);

这里改为

$fontcontent = substr($data,rand(0,strlen($data)-1),1);

原因是php的rand方法为闭区间,前后分别为最小值和最大值,都可以出现

试试strlen($data)-1

$fontcontent = substr($data,rand(0,strlen($data)),1);这行

艾玛我点进来的时候还没有回复。跟楼上雷同,我不是故意的。
大家给楼上点赞吧。

Étiquettes associées:
php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal