php实现的简单中文验证码功能示例

2025-05-29 0 46

本文实例讲述了php实现的简单中文验证码功能。分享给大家供大家参考,具体如下:

img.php

?

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
<?php

session_start();

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

$rand .= dechex(rand(1,15));

}

$_SESSION[check_pic] = $rand;

*/

$image = imagecreatetruecolor(100, 30);

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

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

//imagestring($image, rand(1,6), rand(3,60), rand(3,15), $rand, $color);

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

$color2 = imagecolorallocate($image, rand(0,255), rand(0,255),rand(0,255));

imageline($image, rand(0,100), 0, 100, 30, $color2);

}

//rand() ---->0-max 不大于100

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

imagesetpixel($image, rand()%100, rand()%30, $color2);

}

//$str = iconv("gbk", "utf-8", "中");

$str = "中国";

$_SESSION[check_pic] = $str;

//解决中文,页面本身为utf-8

$str = mb_convert_encoding($str, "html-entities","utf-8" );

//2:字体大小 3:倾斜角度 x , y 坐标

imagettftext($image, 12, 0, 20, 20, $color, 'MSYH.TTF', $str);

//输出图片

header("Content-type: image/jpeg;charset=utf-8");

imagejpeg($image);

/*修改eclipse的配置,可以使得eclipse的新建项目的默认编码直接为UTF-8

在菜单栏的

Window->Preferences->General->Workspace->Text file encoding

将其改为UFT-8即可。*/

?>

sub.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
<?php

header("Content-type: text/html;charset=utf-8");

session_start();

if($_POST[check]) {

if($_POST[check]==$_SESSION[check_pic]) {

echo "验证码正确:".$_SESSION[check_pic];

} else {

echo "验证码错误:".$_SESSION[check_pic];

}

}

?>

<form action="" method="post">

<img id=\"codetool\">

运行效果图如下:

php实现的简单中文验证码功能示例

希望本文所述对大家PHP程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php实现的简单中文验证码功能示例 https://www.kuaiidc.com/95584.html

相关文章

发表评论
暂无评论