首页 > php教程 > php手册 > PHP生成图片验证码demo【OOP面向对象版本】

PHP生成图片验证码demo【OOP面向对象版本】

WBOY
发布: 2016-10-08 16:04:44
原创
1203 人浏览过

下面是我今天下午用PHP写的一个生成图片验证码demo,仅供参考。

这个demo总共分为4个文件,具体代码如下:

1、code.html中的代码:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #ff00ff;">doctype html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><span style="color: #800000;">html </span><span style="color: #ff0000;">lang</span><span style="color: #0000ff;">="en"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 3</span>     <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 4</span>         <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="utf-8"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 5</span>         <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>登录、注册验证码生成<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 6</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 7</span>     <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 8</span>          <span style="color: #008000;"><!--</span>
<span style="color: #008080;"> 9</span> <span style="color: #008000;">             * @Description  网站登录/注册验证码生成类
</span><span style="color: #008080;">10</span> <span style="color: #008000;">             * @Author  赵一鸣
</span><span style="color: #008080;">11</span> <span style="color: #008000;">             * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
</span><span style="color: #008080;">12</span> <span style="color: #008000;">             * @Date  2016年10月6日 
</span><span style="color: #008080;">13</span>          <span style="color: #008000;">--></span>
<span style="color: #008080;">14</span>         <span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="checkcode.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">15</span>             <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="code"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span>
<span style="color: #008080;">16</span>             <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="showcode.php"</span><span style="color: #ff0000;"> onclick</span><span style="color: #0000ff;">="this.setAttribute('src','showcode.php?'+Math.random())"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;">17</span>             <span style="color: #0000ff;"><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>看不清?点击图片即可切换验证码<span style="color: #0000ff;"></span><span style="color: #800000;">span</span><span style="color: #0000ff;">><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span>
<span style="color: #008080;">18</span>             <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sub"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="登录/注册"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;">19</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">20</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">21</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制

2、createcode.class.php中的代码:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">     * @Description  网站登录/注册验证码生成类
</span><span style="color: #008080;"> 4</span> <span style="color: #008000;">     * @Author  赵一鸣
</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">     * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">     * @Date  2016年10月6日 
</span><span style="color: #008080;"> 7</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 8</span>     <span style="color: #0000ff;">class</span><span style="color: #000000;"> Createcode{
</span><span style="color: #008080;"> 9</span>         <span style="color: #008000;">//</span><span style="color: #008000;">画布资源</span>
<span style="color: #008080;">10</span>         <span style="color: #0000ff;">public</span> <span style="color: #800080;">$img</span><span style="color: #000000;">;
</span><span style="color: #008080;">11</span>         <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;">$img_width</span><span style="color: #000000;">;
</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: #0000ff;">private</span> <span style="color: #800080;">$img_height</span><span style="color: #000000;">;
</span><span style="color: #008080;">15</span>         <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;">$img_bgcolor</span><span style="color: #000000;">;
</span><span style="color: #008080;">17</span>         <span style="color: #008000;">//</span><span style="color: #008000;">验证码文字内容</span>
<span style="color: #008080;">18</span>         <span style="color: #0000ff;">private</span> <span style="color: #800080;">$str_content</span><span style="color: #000000;">;
</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: #0000ff;">private</span> <span style="color: #800080;">$code_content</span><span style="color: #000000;">;
</span><span style="color: #008080;">21</span>         <span style="color: #008000;">//</span><span style="color: #008000;">验证码颜色</span>
<span style="color: #008080;">22</span>         <span style="color: #0000ff;">private</span> <span style="color: #800080;">$code_content_color</span><span style="color: #000000;">;
</span><span style="color: #008080;">23</span>         <span style="color: #008000;">//</span><span style="color: #008000;">构造函数</span>
<span style="color: #008080;">24</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$img_width</span>,<span style="color: #800080;">$img_height</span>,<span style="color: #800080;">$str_content</span>,<span style="color: #800080;">$code_content_color</span><span style="color: #000000;">){
</span><span style="color: #008080;">25</span>             <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">gdcheck()){
</span><span style="color: #008080;">26</span>                 <span style="color: #800080;">$this</span>->img_width = <span style="color: #800080;">$img_width</span><span style="color: #000000;">;
</span><span style="color: #008080;">27</span>                 <span style="color: #800080;">$this</span>->img_height = <span style="color: #800080;">$img_height</span><span style="color: #000000;">;
</span><span style="color: #008080;">28</span>                 <span style="color: #800080;">$this</span>->str_content = <span style="color: #800080;">$str_content</span><span style="color: #000000;">;
</span><span style="color: #008080;">29</span>                 <span style="color: #800080;">$this</span>->code_content_color = <span style="color: #800080;">$code_content_color</span><span style="color: #000000;">;
</span><span style="color: #008080;">30</span>                 <span style="color: #800080;">$this</span>-><span style="color: #000000;">get_code();
</span><span style="color: #008080;">31</span>                 <span style="color: #800080;">$this</span>-><span style="color: #000000;">session_code();
</span><span style="color: #008080;">32</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">33</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">34</span>         <span style="color: #008000;">//</span><span style="color: #008000;">生成画布</span>
<span style="color: #008080;">35</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_img(){
</span><span style="color: #008080;">36</span>             <span style="color: #008000;">//</span><span style="color: #008000;">定义画布</span>
<span style="color: #008080;">37</span>             <span style="color: #800080;">$this</span>->img = imagecreatetruecolor(<span style="color: #800080;">$this</span>->img_width, <span style="color: #800080;">$this</span>-><span style="color: #000000;">img_height);
</span><span style="color: #008080;">38</span>             <span style="color: #008000;">//</span><span style="color: #008000;">画布背景色</span>
<span style="color: #008080;">39</span>             <span style="color: #800080;">$this</span>->img_bgcolor = imagecolorallocate(<span style="color: #800080;">$this</span>->img, <span style="color: #008080;">mt_rand</span>(0,255), <span style="color: #008080;">mt_rand</span>(0,255), <span style="color: #008080;">mt_rand</span>(0,255<span style="color: #000000;">));
</span><span style="color: #008080;">40</span>             <span style="color: #008000;">//</span><span style="color: #008000;">给画图填充背景色</span>
<span style="color: #008080;">41</span>             imagefill(<span style="color: #800080;">$this</span>->img, 0, 0, <span style="color: #800080;">$this</span>-><span style="color: #000000;">img_bgcolor);
</span><span style="color: #008080;">42</span>             <span style="color: #008000;">//</span><span style="color: #008000;">取得画布的宽高</span>
<span style="color: #008080;">43</span>             <span style="color: #800080;">$img_width</span> = imagesx(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img);
</span><span style="color: #008080;">44</span>             <span style="color: #800080;">$img_height</span> = imagesy(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img);
</span><span style="color: #008080;">45</span>             <span style="color: #008000;">//</span><span style="color: #008000;">画布中插入验证码</span>
<span style="color: #008080;">46</span>             imagestring(<span style="color: #800080;">$this</span>->img, 5, (<span style="color: #800080;">$this</span>->img_width/3), (<span style="color: #800080;">$this</span>->img_height/2.5), <span style="color: #800080;">$this</span>->code_content, imagecolorallocate(<span style="color: #800080;">$this</span>->img, <span style="color: #008080;">hexdec</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->code_content_color, 1,2)), <span style="color: #008080;">hexdec</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->code_content_color, 3,2)), <span style="color: #008080;">hexdec</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->code_content_color, 5,2<span style="color: #000000;">))));
</span><span style="color: #008080;">47</span>             <span style="color: #008000;">//</span><span style="color: #008000;">画布中插入像素点</span>
<span style="color: #008080;">48</span>             <span style="color: #800080;">$this</span>-><span style="color: #000000;">get_pix();
</span><span style="color: #008080;">49</span>             <span style="color: #008000;">//</span><span style="color: #008000;">画布中插入直线</span>
<span style="color: #008080;">50</span>             <span style="color: #800080;">$this</span>-><span style="color: #000000;">get_line();
</span><span style="color: #008080;">51</span>             <span style="color: #008000;">//</span><span style="color: #008000;">画布显示</span>
<span style="color: #008080;">52</span>             <span style="color: #008080;">header</span>('Content-type:image/png'<span style="color: #000000;">);
</span><span style="color: #008080;">53</span>             imagepng(<span style="color: #800080;">$this</span>-><span style="color: #000000;">img);
</span><span style="color: #008080;">54</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">55</span>         <span style="color: #008000;">//</span><span style="color: #008000;">生成验证码</span>
<span style="color: #008080;">56</span>         <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_code(){
</span><span style="color: #008080;">57</span>             <span style="color: #800080;">$str_content_len</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">str_content);
</span><span style="color: #008080;">58</span>             <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$i++<span style="color: #000000;">){
</span><span style="color: #008080;">59</span>                 <span style="color: #800080;">$this</span>->code_content .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->str_content, <span style="color: #008080;">mt_rand</span>(0,<span style="color: #800080;">$str_content_len</span>-1),1<span style="color: #000000;">);
</span><span style="color: #008080;">60</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">61</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">62</span>         <span style="color: #008000;">//</span><span style="color: #008000;">生成像素点</span>
<span style="color: #008080;">63</span>         <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_pix(){
</span><span style="color: #008080;">64</span>             <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=0;<span style="color: #800080;">$j</span>$j++<span style="color: #000000;">){
</span><span style="color: #008080;">65</span>                 <span style="color: #800080;">$image_pix</span> .= imagesetpixel(<span style="color: #800080;">$this</span>->img, <span style="color: #008080;">mt_rand</span>(0,<span style="color: #800080;">$this</span>->img_width), <span style="color: #008080;">mt_rand</span>(0,<span style="color: #800080;">$this</span>->img_height), imagecolorallocate(<span style="color: #800080;">$this</span>->img, <span style="color: #008080;">mt_rand</span>(0,255), <span style="color: #008080;">mt_rand</span>(0,255), <span style="color: #008080;">mt_rand</span>(0,255<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: #0000ff;">return</span> <span style="color: #800080;">$image_pix</span><span style="color: #000000;">;
</span><span style="color: #008080;">68</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">69</span>         <span style="color: #008000;">//</span><span style="color: #008000;">生成直线</span>
<span style="color: #008080;">70</span>         <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_line(){
</span><span style="color: #008080;">71</span>             <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$l</span>=0;<span style="color: #800080;">$l</span>$l++<span style="color: #000000;">){
</span><span style="color: #008080;">72</span>                 <span style="color: #800080;">$img_line</span> .= imageline(<span style="color: #800080;">$this</span>->img, <span style="color: #008080;">mt_rand</span>(0,<span style="color: #800080;">$this</span>->img_width), <span style="color: #008080;">mt_rand</span>(0,<span style="color: #800080;">$this</span>->img_height), <span style="color: #008080;">mt_rand</span>(0,<span style="color: #800080;">$this</span>->img_width), <span style="color: #008080;">mt_rand</span>(0,<span style="color: #800080;">$this</span>->img_height), imagecolorallocate(<span style="color: #800080;">$this</span>->img, <span style="color: #008080;">mt_rand</span>(0,255), <span style="color: #008080;">mt_rand</span>(0,255), <span style="color: #008080;">mt_rand</span>(0,255<span style="color: #000000;">)));
</span><span style="color: #008080;">73</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">74</span>             <span style="color: #0000ff;">return</span> <span style="color: #800080;">$img_line</span><span style="color: #000000;">;
</span><span style="color: #008080;">75</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">76</span>         <span style="color: #008000;">//</span><span style="color: #008000;">session存储验证码</span>
<span style="color: #008080;">77</span>         <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> session_code(){
</span><span style="color: #008080;">78</span>             <span style="color: #008080;">session_start</span><span style="color: #000000;">();
</span><span style="color: #008080;">79</span>             <span style="color: #800080;">$_SESSION</span>['code'] = <span style="color: #800080;">$this</span>-><span style="color: #000000;">code_content;
</span><span style="color: #008080;">80</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">81</span>         <span style="color: #008000;">//</span><span style="color: #008000;">判断程序是否支持GD库</span>
<span style="color: #008080;">82</span>         <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> gdcheck(){
</span><span style="color: #008080;">83</span>             <span style="color: #0000ff;">if</span>(<span style="color: #008080;">extension_loaded</span>('gd'<span style="color: #000000;">)){
</span><span style="color: #008080;">84</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">85</span>             }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">86</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">87</span>                 <span style="color: #0000ff;">exit</span><span style="color: #000000;">();
</span><span style="color: #008080;">88</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">89</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">90</span>     }
登录后复制

3、checkcode.php中的代码:

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * @Description  网站登录/注册验证码生成类
 * @Author  赵一鸣
 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 * @Date  2016年10月6日
 </span><span style="color: #008000;">*/</span>
    <span style="color: #008080;">header</span>('Content-type:text/html;charset="utf-8"'<span style="color: #000000;">);
    </span><span style="color: #008080;">session_start</span><span style="color: #000000;">();
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_POST</span>['code']!=''<span style="color: #000000;">){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_SESSION</span>['code']==<span style="color: #800080;">$_POST</span>['code'<span style="color: #000000;">]){
            </span><span style="color: #0000ff;">echo</span> '<span style="color: #000000;"><script type="text/javascript">
                    alert("验证码填写成功");
                    history.go(-1);
                </script></span>'<span style="color: #000000;">;
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #0000ff;">echo</span> '<span style="color: #000000;"><script type="text/javascript">
                    alert("验证码填写失败");
                    history.go(-1);
                </script></span>'<span style="color: #000000;">;
        }
    }</span>
登录后复制

4、showcode.php中的代码:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;"> * @Description  网站登录/注册验证码生成类
</span><span style="color: #008080;"> 4</span> <span style="color: #008000;"> * @Author  赵一鸣
</span><span style="color: #008080;"> 5</span> <span style="color: #008000;"> * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
</span><span style="color: #008080;"> 6</span> <span style="color: #008000;"> * @Date  2016年10月6日
</span><span style="color: #008080;"> 7</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 8</span>     <span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$classname</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 9</span>         <span style="color: #0000ff;">include</span> <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$classname</span>).'.class.php'<span style="color: #000000;">;
</span><span style="color: #008080;">10</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">11</span>     <span style="color: #008000;">//</span><span style="color: #008000;">定义验证码的取值范围</span>
<span style="color: #008080;">12</span>     <span style="color: #800080;">$str_content</span> = 'abcdefghijklmnopqrstuvwxyz0123456789'<span style="color: #000000;">;
</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_content_color</span> = '#ffffff'<span style="color: #000000;">;
</span><span style="color: #008080;">15</span>     <span style="color: #008000;">//</span><span style="color: #008000;">初始化对象</span>
<span style="color: #008080;">16</span>     <span style="color: #800080;">$code</span> = <span style="color: #0000ff;">new</span> Createcode(100,30,<span style="color: #800080;">$str_content</span>,<span style="color: #800080;">$code_content_color</span><span style="color: #000000;">);
</span><span style="color: #008080;">17</span>     <span style="color: #800080;">$code</span>->get_img();
登录后复制

原文地址:http://www.zymseo.com/php/334.html

转载请注明出处!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板