目录
php生成图片验证码
首页 后端开发 php教程 php生成图片验证码_PHP教程

php生成图片验证码_PHP教程

Jul 13, 2016 am 09:51 AM
php web 图片 应用 生成 用户 重要 防止 验证

php生成图片验证码

   验证码在WEB应用中非常重要,通常用来防止用户恶意提交表单,如恶意注册和登录、论坛恶意灌水等。本文将通过实例讲解使用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

$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

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启用gd库GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。

// 在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。

session_start();

 

// 把GBK编码的字符串转换成UTF-8字符串,第一个参数之所以写GBK,是因为本php文件在主机中存储的编码是GBK编码

// UTF-8编码浏览器普遍支持,通用性强,这里就转换成UTF-8

$str = iconv("GBK", "utf-8", "芸芸众生绿水青山名胜古迹敞开心胸便会云蒸霞蔚快乐将永远伴随着你");

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

{

exit("不是字符串或者不是utf-8");

}

$zhongwenku_size;

// 按UTF-8编码方式获取字符串的长度

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

 

// 把上述字符导入数组中

$zhongwenku = array();

for( $i=0; $i

{

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

}

 

$result = "";

 

// 图片上要写入的四个字符

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;

 

// 创建一个真彩图片 宽100,高30

$img = imagecreatetruecolor(100, 30);

 

// 分配背景颜色

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

 

// 分配文字颜色

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

 

// 在图片上写字符串

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

 

// 在图片上根据载入字体可以写出特殊字体

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));

// 画线

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));

// 为图片添加噪点

for($i=0; $i

{

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

}

// 发送http头信息 指定本次发送的是image中的jpeg

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

// 输出jpeg图片至浏览器

imagejpeg($img);

 

?>

  再来一个实例吧

  ?

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

php生成图片验证码_PHP教程

  如果想实现 "看不清?换一张" 效果,添加如下 JS 到页面中

  ?

1

2

3

function changing(){

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

}

  以上所述就是本文的全部内容了,希望大家能够喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1013643.htmlTechArticlephp生成图片验证码 验证码在WEB应用中非常重要,通常用来防止用户恶意提交表单,如恶意注册和登录、论坛恶意灌水等。本文将通过实例讲解...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 使用数据库 CakePHP 使用数据库 Sep 10, 2024 pm 05:25 PM

在 CakePHP 中使用数据库非常容易。本章我们将了解CRUD(创建、读取、更新、删除)操作。

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

CakePHP 日志记录 CakePHP 日志记录 Sep 10, 2024 pm 05:26 PM

登录 CakePHP 是一项非常简单的任务。您只需使用一项功能即可。您可以记录任何后台进程(如 cronjob)的错误、异常、用户活动、用户采取的操作。在 CakePHP 中记录数据很容易。提供了 log() 函数

See all articles