Maison >php教程 >php手册 >John细说PHP的验证码

John细说PHP的验证码

WBOY
WBOYoriginal
2016-08-29 08:36:491078parcourir

细说php中的验证码类创建

我这里自己写了一个验证码类,我来演示一下怎么使用,我是菜鸟一枚,大神请略过。我来讲解一下它的使用方法,总共需要两步即可。

第一步:

下载我制作好的验证码类。下载地址:http://files.cnblogs.com/files/xfjpeter/Verify.zip

第二步:

 1.创建一个字的验证码文件

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> 
<span style="color: #008080;"> 3</span> <span style="color: #008000;">#</span><span style="color: #008000;">引入验证码类文件</span>
<span style="color: #008080;"> 4</span> <span style="color: #0000ff;">require_once</span>('Verify.class.php'<span style="color: #000000;">);
</span><span style="color: #008080;"> 5</span> 
<span style="color: #008080;"> 6</span> <span style="color: #008000;">#</span><span style="color: #008000;">实例化验证码类</span>
<span style="color: #008080;"> 7</span> <span style="color: #008000;">#</span><span style="color: #008000;">初始化的使用可以传四个参数,分别是:验证码图片的长、高,验证码的长度,验证码的类型(验证码的类型需要将bgRand属性设置为false)</span>
<span style="color: #008080;"> 8</span> <span style="color: #800080;">$code</span> = <span style="color: #0000ff;">new</span> Verify(140, 40, 6, 6<span style="color: #000000;">);
</span><span style="color: #008080;"> 9</span> 
<span style="color: #008080;">10</span> <span style="color: #008000;">#</span><span style="color: #008000;">设置验证码图片的长度</span>
<span style="color: #008080;">11</span> <span style="color: #800080;">$code</span> -> width = 200<span style="color: #000000;">;
</span><span style="color: #008080;">12</span> 
<span style="color: #008080;">13</span> <span style="color: #008000;">#</span><span style="color: #008000;">设置验证码图片的高度</span>
<span style="color: #008080;">14</span> <span style="color: #800080;">$code</span> -> height = 60<span style="color: #000000;">;
</span><span style="color: #008080;">15</span> 
<span style="color: #008080;">16</span> <span style="color: #008000;">#</span><span style="color: #008000;">是否随机背景,默认true(随机)</span>
<span style="color: #008080;">17</span> <span style="color: #800080;">$code</span> -> bgRand = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">18</span> 
<span style="color: #008080;">19</span> <span style="color: #008000;">#</span><span style="color: #008000;">显示验证码</span>
<span style="color: #008080;">20</span> <span style="color: #800080;">$code</span> -> verify();

 生成的图片样式为如图

 

2.验证码类文件为

<span style="color: #008080;">  1</span> <span style="color: #000000;">php
</span><span style="color: #008080;">  2</span> 
<span style="color: #008080;">  3</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">  4</span> <span style="color: #008000;"> * 验证码类
</span><span style="color: #008080;">  5</span> <span style="color: #008000;"> * @author John <fsyzxz>
</fsyzxz></span><span style="color: #008080;">  6</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;">  7</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Verify
</span><span style="color: #008080;">  8</span> <span style="color: #000000;">{
</span><span style="color: #008080;">  9</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$width</span> = 160;       <span style="color: #008000;">//</span><span style="color: #008000;">验证码的宽度</span>
<span style="color: #008080;"> 10</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$height</span> = 60;       <span style="color: #008000;">//</span><span style="color: #008000;">验证码的高度</span>
<span style="color: #008080;"> 11</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$type</span> = 1;          <span style="color: #008000;">//</span><span style="color: #008000;">验证码的类型</span>
<span style="color: #008080;"> 12</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$length</span> = 4;        <span style="color: #008000;">//</span><span style="color: #008000;">验证码的长度</span>
<span style="color: #008080;"> 13</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$code</span>;              <span style="color: #008000;">//</span><span style="color: #008000;">验证码</span>
<span style="color: #008080;"> 14</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$img</span>;               <span style="color: #008000;">//</span><span style="color: #008000;">图像的资源</span>
<span style="color: #008080;"> 15</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$seKey</span> = 'John';    <span style="color: #008000;">//</span><span style="color: #008000;">密钥</span>
<span style="color: #008080;"> 16</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$bgRand</span> = <span style="color: #0000ff;">true</span>;     <span style="color: #008000;">//</span><span style="color: #008000;">随机背景图片</span>
<span style="color: #008080;"> 17</span>     
<span style="color: #008080;"> 18</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 19</span> <span style="color: #008000;">     * 构造函数
</span><span style="color: #008080;"> 20</span> <span style="color: #008000;">     * @param type $width 验证码的宽度
</span><span style="color: #008080;"> 21</span> <span style="color: #008000;">     * @param type $height 验证码的高度
</span><span style="color: #008080;"> 22</span> <span style="color: #008000;">     * @param type $length 验证码的长度
</span><span style="color: #008080;"> 23</span> <span style="color: #008000;">     * @param type $type 验证码的类型
</span><span style="color: #008080;"> 24</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 25</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$width</span> = 160, <span style="color: #800080;">$height</span> = 40, <span style="color: #800080;">$length</span> = 4, <span style="color: #800080;">$type</span> = 1<span style="color: #000000;">)
</span><span style="color: #008080;"> 26</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 27</span>         <span style="color: #800080;">$this</span>->width  = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$width</span>)  ? <span style="color: #800080;">$width</span>  : <span style="color: #800080;">$this</span>-><span style="color: #000000;">width;
</span><span style="color: #008080;"> 28</span>         <span style="color: #800080;">$this</span>->height = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$height</span>) ? <span style="color: #800080;">$height</span> : <span style="color: #800080;">$this</span>-><span style="color: #000000;">height;
</span><span style="color: #008080;"> 29</span>         <span style="color: #800080;">$this</span>->length = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$length</span>) ? <span style="color: #800080;">$length</span> : <span style="color: #800080;">$this</span>-><span style="color: #000000;">length;
</span><span style="color: #008080;"> 30</span>         <span style="color: #800080;">$this</span>->type   = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$type</span>)   ? <span style="color: #800080;">$type</span>   : <span style="color: #800080;">$this</span>-><span style="color: #000000;">type;
</span><span style="color: #008080;"> 31</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 32</span>     
<span style="color: #008080;"> 33</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 34</span> <span style="color: #008000;">     * 设置属性值
</span><span style="color: #008080;"> 35</span> <span style="color: #008000;">     * @param type $name 属性名
</span><span style="color: #008080;"> 36</span> <span style="color: #008000;">     * @param type $value 属性值
</span><span style="color: #008080;"> 37</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 38</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __set(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$value</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 39</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 40</span>         <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$name</span><span style="color: #000000;">)) {
</span><span style="color: #008080;"> 41</span>             <span style="color: #800080;">$this</span>-><span style="color: #800080;">$name</span> = <span style="color: #800080;">$value</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 42</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 43</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 44</span>     
<span style="color: #008080;"> 45</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 46</span> <span style="color: #008000;">     * 获取属性值
</span><span style="color: #008080;"> 47</span> <span style="color: #008000;">     * @param type $name 属性名
</span><span style="color: #008080;"> 48</span> <span style="color: #008000;">     * @return type 返回属性值
</span><span style="color: #008080;"> 49</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 50</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __get(<span style="color: #800080;">$name</span><span style="color: #000000;">) {
</span><span style="color: #008080;"> 51</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #800080;">$name</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 52</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 53</span>     
<span style="color: #008080;"> 54</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 55</span> <span style="color: #008000;">     * 校验验证码
</span><span style="color: #008080;"> 56</span> <span style="color: #008000;">     * @param type $code 表单提供的验证码
</span><span style="color: #008080;"> 57</span> <span style="color: #008000;">     * @return boolean
</span><span style="color: #008080;"> 58</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 59</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> check(<span style="color: #800080;">$code</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 60</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SESSION</span>)) {<span style="color: #008080;">session_start</span><span style="color: #000000;">();}
</span><span style="color: #008080;"> 61</span>         <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$this</span>->encodeVerify(<span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$code</span>)) === <span style="color: #800080;">$_SESSION</span>['code'<span style="color: #000000;">]){
</span><span style="color: #008080;"> 62</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 63</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;"> 64</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 65</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 66</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 67</span>     
<span style="color: #008080;"> 68</span>     <span style="color: #008000;">//</span><span style="color: #008000;">输出验证码</span>
<span style="color: #008080;"> 69</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> verify()
</span><span style="color: #008080;"> 70</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 71</span>         <span style="color: #800080;">$this</span>->code = <span style="color: #800080;">$this</span>-><span style="color: #000000;">createVerify();
</span><span style="color: #008080;"> 72</span>         <span style="color: #008000;">//</span><span style="color: #008000;">创建背景</span>
<span style="color: #008080;"> 73</span>         <span style="color: #800080;">$this</span>-><span style="color: #000000;">createBackground();
</span><span style="color: #008080;"> 74</span>         <span style="color: #008000;">//</span><span style="color: #008000;">文字显示</span>
<span style="color: #008080;"> 75</span>         <span style="color: #800080;">$this</span>-><span style="color: #000000;">writeString();
</span><span style="color: #008080;"> 76</span>         <span style="color: #008000;">//</span><span style="color: #008000;">画干扰线</span>
<span style="color: #008080;"> 77</span>         <span style="color: #800080;">$this</span>-><span style="color: #000000;">paitLine();
</span><span style="color: #008080;"> 78</span>         <span style="color: #008000;">//</span><span style="color: #008000;">输入图像</span>
<span style="color: #008080;"> 79</span>         <span style="color: #800080;">$this</span>-><span style="color: #000000;">printImg();
</span><span style="color: #008080;"> 80</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 81</span>     
<span style="color: #008080;"> 82</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 83</span> <span style="color: #008000;">     * 创建背景图片
</span><span style="color: #008080;"> 84</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 85</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> createBackground()
</span><span style="color: #008080;"> 86</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 87</span>         <span style="color: #008000;">//</span><span style="color: #008000;">从图片库创建一个图像, 判断是否随机</span>
<span style="color: #008080;"> 88</span>         <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$this</span>-><span style="color: #000000;">bgRand){
</span><span style="color: #008080;"> 89</span>             <span style="color: #800080;">$img</span> = imagecreatefromjpeg('./verify/bgs/'.<span style="color: #008080;">mt_rand</span>(1,8).'.jpg'<span style="color: #000000;">);
</span><span style="color: #008080;"> 90</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;"> 91</span>             <span style="color: #800080;">$img</span> = imagecreatefromjpeg('./verify/bgs/'.<span style="color: #800080;">$this</span>->type.'.jpg'<span style="color: #000000;">);
</span><span style="color: #008080;"> 92</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 93</span>         <span style="color: #008000;">//</span><span style="color: #008000;">创建一个图片</span>
<span style="color: #008080;"> 94</span>         <span style="color: #800080;">$this</span>->img = imagecreatetruecolor(<span style="color: #800080;">$this</span>->width, <span style="color: #800080;">$this</span>-><span style="color: #000000;">height);
</span><span style="color: #008080;"> 95</span>         <span style="color: #008000;">//</span><span style="color: #008000;">把图片复制到创建的图像上</span>
<span style="color: #008080;"> 96</span>         imagecopyresampled(<span style="color: #800080;">$this</span>->img, <span style="color: #800080;">$img</span>, 0, 0, 0, 0, <span style="color: #800080;">$this</span>->width, <span style="color: #800080;">$this</span>->height, imagesx(<span style="color: #800080;">$img</span>), imagesy(<span style="color: #800080;">$img</span><span style="color: #000000;">));
</span><span style="color: #008080;"> 97</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 98</span>     
<span style="color: #008080;"> 99</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">100</span> <span style="color: #008000;">     * 在图片上写字
</span><span style="color: #008080;">101</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">102</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> writeString()
</span><span style="color: #008080;">103</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">104</span>         <span style="color: #800080;">$color</span> = imagecolorallocatealpha(<span style="color: #800080;">$this</span>->img, <span style="color: #008080;">mt_rand</span>(0,128), <span style="color: #008080;">mt_rand</span>(0,128), <span style="color: #008080;">mt_rand</span>(0,128), 0<span style="color: #000000;">);
</span><span style="color: #008080;">105</span>         <span style="color: #800080;">$fontType</span> = './verify/ttfs/'.<span style="color: #008080;">mt_rand</span>(1,6).'.ttf'<span style="color: #000000;">;
</span><span style="color: #008080;">106</span>         <span style="color: #800080;">$fontSize</span> = <span style="color: #008080;">mt_rand</span>(15, 20<span style="color: #000000;">);
</span><span style="color: #008080;">107</span>         <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $this->length; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) {
</span><span style="color: #008080;">108</span>             <span style="color: #800080;">$x</span> = 3+(<span style="color: #800080;">$this</span>->width/<span style="color: #800080;">$this</span>->length)*<span style="color: #800080;">$i</span><span style="color: #000000;">;
</span><span style="color: #008080;">109</span>             <span style="color: #800080;">$y</span> = <span style="color: #008080;">mt_rand</span>((<span style="color: #800080;">$this</span>->height/3)*2, (<span style="color: #800080;">$this</span>->height/3)*2<span style="color: #000000;">);
</span><span style="color: #008080;">110</span>             <span style="color: #008000;">//</span><span style="color: #008000;">把验证码写在图片上</span>
<span style="color: #008080;">111</span>             imagettftext(<span style="color: #800080;">$this</span>->img, <span style="color: #800080;">$fontSize</span>, 0, <span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, <span style="color: #800080;">$color</span>, <span style="color: #800080;">$fontType</span>, <span style="color: #800080;">$this</span>->code[<span style="color: #800080;">$i</span><span style="color: #000000;">]);
</span><span style="color: #008080;">112</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">113</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">114</span>     
<span style="color: #008080;">115</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">116</span> <span style="color: #008000;">     * 画干扰线和字母
</span><span style="color: #008080;">117</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">118</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> paitLine()
</span><span style="color: #008080;">119</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">120</span>         <span style="color: #800080;">$px</span> = <span style="color: #800080;">$py</span> = 0<span style="color: #000000;">;
</span><span style="color: #008080;">121</span>         <span style="color: #800080;">$codes</span> = '2345678abcdefhijkmnpqrstuvwxyz'<span style="color: #000000;">;
</span><span style="color: #008080;">122</span>         <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $this->width/4; <span style="color: #800080;">$i</span>++<span style="color: #000000;">){
</span><span style="color: #008080;">123</span>             <span style="color: #800080;">$num</span> = <span style="color: #008080;">mt_rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$codes</span>)-1<span style="color: #000000;">);
</span><span style="color: #008080;">124</span>             <span style="color: #800080;">$color</span> = imagecolorallocatealpha(<span style="color: #800080;">$this</span>->img, 255, 255, 255, 80<span style="color: #000000;">);
</span><span style="color: #008080;">125</span>             <span style="color: #008000;">//</span><span style="color: #008000;">画字母</span>
<span style="color: #008080;">126</span>             imagechar(<span style="color: #800080;">$this</span>->img, 8, <span style="color: #008080;">mt_rand</span>(3, <span style="color: #800080;">$this</span>->width), <span style="color: #008080;">mt_rand</span>(3, <span style="color: #800080;">$this</span>->height), <span style="color: #800080;">$codes</span>{<span style="color: #800080;">$num</span>}, <span style="color: #800080;">$color</span><span style="color: #000000;">);
</span><span style="color: #008080;">127</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">128</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">129</span>     
<span style="color: #008080;">130</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">131</span> <span style="color: #008000;">     * 输入图像
</span><span style="color: #008080;">132</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">133</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> printImg()
</span><span style="color: #008080;">134</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">135</span>         <span style="color: #0000ff;">if</span>(<span style="color: #008080;">function_exists</span>('imagegif'<span style="color: #000000;">)){
</span><span style="color: #008080;">136</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 针对 GIF</span>
<span style="color: #008080;">137</span>             <span style="color: #008080;">header</span>('Content-Type: image/gif'<span style="color: #000000;">);
</span><span style="color: #008080;">138</span>             imagegif(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img);
</span><span style="color: #008080;">139</span>         }<span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">function_exists</span>('imagejpeg'<span style="color: #000000;">)){
</span><span style="color: #008080;">140</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 针对 JPEG</span>
<span style="color: #008080;">141</span>             <span style="color: #008080;">header</span>('Content-Type: image/jpeg'<span style="color: #000000;">);
</span><span style="color: #008080;">142</span>             imagejpeg(<span style="color: #800080;">$this</span>->img, <span style="color: #0000ff;">NULL</span>, 100<span style="color: #000000;">);
</span><span style="color: #008080;">143</span>         }<span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">function_exists</span>('imagepng'<span style="color: #000000;">)){
</span><span style="color: #008080;">144</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 针对 PNG</span>
<span style="color: #008080;">145</span>             <span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">);
</span><span style="color: #008080;">146</span>             imagepng(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img);
</span><span style="color: #008080;">147</span>         }<span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">function_exists</span>('imagewbmp'<span style="color: #000000;">)){
</span><span style="color: #008080;">148</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 针对 WBMP</span>
<span style="color: #008080;">149</span>             <span style="color: #008080;">header</span>('Content-Type: image/vnd.wap.wbmp'<span style="color: #000000;">);
</span><span style="color: #008080;">150</span>             imagewbmp(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img);
</span><span style="color: #008080;">151</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">152</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">153</span>     
<span style="color: #008080;">154</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">155</span> <span style="color: #008000;">     * 生成验证码
</span><span style="color: #008080;">156</span> <span style="color: #008000;">     * @return string 返回生成的验证码
</span><span style="color: #008080;">157</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">158</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> createVerify()
</span><span style="color: #008080;">159</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">160</span>         <span style="color: #800080;">$codeSet</span> = '2345678abcdefhijkmnpqrstuvwxyz'<span style="color: #000000;">;
</span><span style="color: #008080;">161</span>         <span style="color: #800080;">$codes</span> = ''<span style="color: #000000;">;
</span><span style="color: #008080;">162</span>         <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $this->length; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) {
</span><span style="color: #008080;">163</span>             <span style="color: #800080;">$codes</span> .= <span style="color: #800080;">$codeSet</span>[<span style="color: #008080;">mt_rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$codeSet</span>)-1<span style="color: #000000;">)];
</span><span style="color: #008080;">164</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">165</span>         <span style="color: #008000;">//</span><span style="color: #008000;">把验证码保存到session中</span>
<span style="color: #008080;">166</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SESSION</span>)) {<span style="color: #008080;">session_start</span><span style="color: #000000;">();}
</span><span style="color: #008080;">167</span>         <span style="color: #800080;">$_SESSION</span>['code'] = <span style="color: #800080;">$this</span>->encodeVerify(<span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$codes</span><span style="color: #000000;">));
</span><span style="color: #008080;">168</span> <span style="color: #008000;">//</span><span style="color: #008000;">        $_SESSION['code'] = $codes;</span>
<span style="color: #008080;">169</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$codes</span><span style="color: #000000;">;
</span><span style="color: #008080;">170</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">171</span>     
<span style="color: #008080;">172</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">173</span> <span style="color: #008000;">     * 加密验证码
</span><span style="color: #008080;">174</span> <span style="color: #008000;">     * @param type $string
</span><span style="color: #008080;">175</span> <span style="color: #008000;">     * @return type
</span><span style="color: #008080;">176</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">177</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> encodeVerify(<span style="color: #800080;">$string</span><span style="color: #000000;">)
</span><span style="color: #008080;">178</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">179</span>         <span style="color: #800080;">$key</span> = <span style="color: #008080;">substr</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$this</span>->seKey), 5, 8<span style="color: #000000;">);
</span><span style="color: #008080;">180</span>         <span style="color: #800080;">$str</span> = <span style="color: #008080;">substr</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$string</span>), 8, 10<span style="color: #000000;">);
</span><span style="color: #008080;">181</span>         <span style="color: #0000ff;">return</span> <span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span> . <span style="color: #800080;">$str</span><span style="color: #000000;">);
</span><span style="color: #008080;">182</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">183</span>     
<span style="color: #008080;">184</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">185</span> <span style="color: #008000;">     * 销毁图像
</span><span style="color: #008080;">186</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">187</span>     <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct()
</span><span style="color: #008080;">188</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">189</span>         <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img)){
</span><span style="color: #008080;">190</span>             imagedestroy(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img);
</span><span style="color: #008080;">191</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">192</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">193</span> }

 以上两步即可生生你想要的验证。

另外说明,Verify.class.php中有一个验证验证码是否正确的方法,使用如下

将你从界面中获得的验证码传入code方法中即可

<span style="color: #0000ff;">if</span> (<span style="color: #800080;">$code</span> -><span style="color: #000000;"> code(这是传入你页面中获取的验证码值)){
    </span><span style="color: #008000;">#</span><span style="color: #008000;">这是验证正确的操作</span>
}<span style="color: #0000ff;">else</span><span style="color: #000000;">{
    </span><span style="color: #008000;">#</span><span style="color: #008000;">验证失败的操作</span>
}

以上就是我创建整个验证码的心得,希望对点击进来看的人有帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn