PHP 用session与gd库实现简单验证码生成与验证的类方法

2025-05-29 0 95

验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的。目前大大小小的网站都有验证码。今天自己实现了一个简单的验证码类。说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下。当然,因为字体的原因,要想一眼看出来并不容易。同时,为了避免字母的大小写与数字混淆,又去掉了那些看起来很像的字母数字。
类:

?

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

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96
<?php

/**

*简单生成验证码类

*/

class Captcha {

private $width;//验证码宽度

private $height;//验证码高度

private $countOfChars;//字符数

//private $distrubLines;//干扰线条数

private $chars;//随机生成的字符串

public function __construct($width=100,$height=30,$countOfChars=4,$distrubLines=2) {

//初始化参数

$this->width=$width;

$this->height=$height;

$this->countOfChars=$countOfChars;

session_start();

}

/**

* 执行全部动作,生成验证码并直接输出

*/

public function execute(){

$imageHandle=$this->createImage();

$this->createChars();

$this->drawChars($imageHandle);

$this->outImage($imageHandle);

}

/**

* 创建画布,并随机填充颜色

* @return 返回画布句柄

*/

public function createImage(){

$imageHandle= imagecreate($this->width, $this->height);

//随机背景颜色

$randColor=imagecolorallocate($imageHandle, 50, mt_rand(0, 50), mt_rand(0, 50));

imagefill($imageHandle, 0, 0, $randColor);

return $imageHandle;

}

/**

* 生成随机字符

*/

private function createChars(){

//候选字符

$str='ABCDEFGHJKLMNPQRSTUVWXZabcdefghijkmnpqtuvwx2346789';

$chars='';

for($i=0;$i<$this->countOfChars;$i++){

$chars.=$str[mt_rand(0,strlen($str)-1)];

}

$this->chars=$chars;

//保存在会话中

$_SESSION['captcha']=strtolower($chars);

}

/**

* 将字符写入图像

* @param type $imageHandle 图像句柄

*/

private function drawChars($imageHandle){

if($this->chars!=null){

$font='/home/WWW/YuWeiLiShuFT.ttf';

for($i=0;$i<strlen($this->chars);$i++){

$color= imagecolorallocate($imageHandle,mt_rand(50, 200),mt_rand(100, 255),255);

imagefttext($imageHandle,30, 30,$i*20+10,25,$color,$font,$this->chars[$i]);

}

}

}

/**

* 输出图像

* @param type $imageHandle 图像句柄

*/

private function outImage($imageHandle){

imagepng($imageHandle);

imagedestroy($imageHandle);

}

/**

* 判断用户输入的验证码是否正确

* @param type $usrInput 用户的输入

* @return boolean 验证码是否匹配

*/

public static function isRight($usrInput){

if(isset($_SESSION['captcha'])){

if(strtolower($usrInput)==$_SESSION['captcha']){

$_SESSION['captcha']=null;

return true;

}else{

$_SESSION['captcha']=null;

return false;

}

}

}

}

把验证设置成了静态方法,因为生成验证码后已经把验证码存到了session中,验证时直接调用静态方法,而不需要实例化这个类了。

上面的字体可以随意设置。

下面的代码讲返回一个图像,实例化Captcha类后动态生成的一个图像。(outCaptcha.php

?

1

2

3

4

5

6
<?php

require('Captcha.php');

$code= new Captcha();

header('Content-Type:image/png');

$code->execute();

header(‘Content-Type:image/png');

这句话的作用是告诉浏览器输出的是png图像,而不是html代码。浏览器收到后就将下面的输出解析成图像。

然后写一个html静态页面(testCaptcha.html),创建表单

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
<!DOCTYPE html>

<html>

<head>

<title>验证码测试</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<h1>请输入验证码:</h1>

<img src="outCaptcha.php"/>

<form method="POST" action="prove.php">

<input type="text" name="input_captcha"/>

<button name="submit">确定</button>

</form>

</body>

</html>

仅仅是这样是不够的,看到表单提交的地址了么?那个就是用来验证验证码是否输入正确的代码:

?

1

2

3

4

5

6

7

8

9
session_start();

$inputCaptcha= trim($_POST['input_captcha']);

require('Captcha.php');

if(Captcha::isRight($inputCaptcha)){

echo '验证码正确';

}else{

echo '验证码错误或已过期';

}

session_destroy();

这里还是要导入Captcha这个类,然后调用其静态方法来验证你的输入。最后销毁整个会话。

最后看看效果吧

PHP 用session与gd库实现简单验证码生成与验证的类方法

太好了,成功了。那再故意输错试试,后退一下,然后刷新(如果不刷新浏览器将直接调用缓存中的验证码图片,这个时候我们的验证码还没有生成呢!所以无论怎么样都出错)。

PHP 用session与gd库实现简单验证码生成与验证的类方法

当然,真正的验证码是可以单击换一张的,这利用了ajax技术。

以上这篇PHP 用session与gd库实现简单验证码生成与验证的类方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 PHP 用session与gd库实现简单验证码生成与验证的类方法 https://www.kuaiidc.com/96282.html

相关文章

发表评论
暂无评论