本文实例讲述了CodeIgniter框架验证码类库文件与用法。分享给大家供大家参考,具体如下:
折腾了我四五个小时,终于,ci的验证码类库成功的整出来了。
下面请看源码:
在application/libraries建立Authcode.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
<?php class Authcode { var $CI ; var $fontPath ; //字体路径 var $image ; var $charLen = 4; //生成几位验证码 var $arrChr = array (); //验证码字符 var $width = 83; //图片宽 var $height = 24; //图片高 var $bgcolor = "#ffffff" ; //背景色 var $showNoisePix = true; //生成杂点 var $noiseNumPix = 80; //生成杂点数量 var $showNoiseLine = true; //生成杂线 var $noiseNumLine = 2; //生成杂线数量 var $showBorder = true; //边框,当杂点、线一起作用的时候,边框容易受干扰 var $borderColor = "#000000" ; function Authcode() { $this ->CI = & get_instance(); $this ->fontPath = realpath (dirname( __FILE__ ) . '/fonts/' ); //字体文件 //$this->arrChr = array_merge(range(1, 9) , range('A', 'Z'));//数字字母验证码 //$this->arrChr = range('A', 'Z');//纯字母验证码 $this ->arrChr = range(0, 9); //纯数字验证码 } /** * 显示验证码 * */ function show() { $this ->image = imageCreate( $this ->width, $this ->height); $this ->back = $this ->getColor( $this ->bgcolor); imageFilledRectangle( $this ->image, 0, 0, $this ->width, $this ->height, $this ->back); $size = $this ->width / $this ->charLen - 4; if ( $size > $this ->height) { $size = $this ->height; } $left = ( $this ->width - $this ->charLen * ( $size + $size / 10)) / $size + 5; $code = '' ; for ( $i = 0; $i < $this ->charLen; $i ++) { $randKey = rand(0, count ( $this ->arrChr) - 1); $randText = $this ->arrChr[ $randKey ]; $code .= $randText ; $textColor = imageColorAllocate( $this ->image, rand(0, 100), rand(0, 100), rand(0, 100)); $font = $this ->fontPath . '/' . rand(1, 5) . ".ttf" ; $randsize = rand( $size - $size / 10, $size + $size / 10); $location = $left + ( $i * $size + $size / 10); @imagettftext( $this ->image, $randsize , rand(- 18, 18), $location , rand( $size - $size / 10, $size + $size / 10) + 2, $textColor , $font , $randText ); } if ( $this ->showNoisePix == true) { $this ->setNoisePix(); } if ( $this ->showNoiseLine == true) { $this ->setNoiseLine(); } if ( $this ->showBorder == true) { $this ->borderColor = $this ->getColor( $this ->borderColor); imageRectangle( $this ->image, 0, 0, $this ->width - 1, $this ->height - 1, $this ->borderColor); } $this ->CI->session->set_userdata( 'auth_code' , $code ); ob_clean(); header( "Content-type: image/jpeg" ); imagejpeg( $this ->image); imagedestroy( $this ->image); } /** * 显示验证码的JS调用 * */ function showScript() { //显示验证码 echo "var img_src = '/imgauthcode/show/?';\n" ; echo "document.writeln('<img id=\"img_authcode\" src=\"' + img_src + Math.random() + '\" style=\"cursor:hand;\" onclick=\"this.src=img_src + Math.random();\" id="codetool">
Authcode.php代码结束 在Controller中,有个admin类,其中有两个方法:
下面是在视图view中创建一个demo.php了,代码如下:
OK. 一切结束,终于正常运行了。 希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。 相关文章
热门资讯 |