搜尋
首頁後端開發php教程php生成图片验证码_PHP教程
php生成图片验证码_PHP教程Jul 13, 2016 am 09:51 AM
phpweb圖片應用產生使用者重要防止驗證

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
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怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

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

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 24, 2022 am 11:49 AM

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

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

熱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.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版