Table of Contents
PHP验证码,php验证码代码
Home php教程 php手册 PHP验证码,php验证码代码

PHP验证码,php验证码代码

Jun 13, 2016 am 09:09 AM
Website backend

PHP验证码,php验证码代码

设计一个验证码类,在需要的时候可以随时调用

验证码类,保存为ValidateCode.class.php

<span> 1</span> <?<span>php
</span><span> 2</span> <span>//</span><span>验证码类</span>
<span> 3</span> <span>session_start</span><span>();
</span><span> 4</span> <span>class</span><span> ValidateCode {
</span><span> 5</span>  <span>private</span> <span>$charset</span> = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';<span>//</span><span>随机因子</span>
<span> 6</span>  <span>private</span> <span>$code</span>;<span>//</span><span>验证码</span>
<span> 7</span>  <span>private</span> <span>$codelen</span> = 4;<span>//</span><span>验证码长度</span>
<span> 8</span>  <span>private</span> <span>$width</span> = 130;<span>//</span><span>宽度</span>
<span> 9</span>  <span>private</span> <span>$height</span> = 50;<span>//</span><span>高度</span>
<span>10</span>  <span>private</span> <span>$img</span>;<span>//</span><span>图形资源句柄</span>
<span>11</span>  <span>private</span> <span>$font</span>;<span>//</span><span>指定的字体</span>
<span>12</span>  <span>private</span> <span>$fontsize</span> = 20;<span>//</span><span>指定字体大小</span>
<span>13</span>  <span>private</span> <span>$fontcolor</span>;<span>//</span><span>指定字体颜色
</span><span>14</span> <span> //构造方法初始化</span>
<span>15</span>  <span>public</span> <span>function</span><span> __construct() {
</span><span>16</span>   <span>$this</span>->font = './latha.ttf';<span>//</span><span>注意字体路径要写对,否则显示不了图片</span>
<span>17</span> <span> }
</span><span>18</span>  <span>//</span><span>生成随机码</span>
<span>19</span>  <span>private</span> <span>function</span><span> createCode() {
</span><span>20</span>   <span>$_len</span> = <span>strlen</span>(<span>$this</span>->charset)-1<span>;
</span><span>21</span>   <span>for</span> (<span>$i</span>=0;<span>$i</span><<span>$this</span>->codelen;<span>$i</span>++<span>) {
</span><span>22</span>    <span>$this</span>->code .= <span>$this</span>->charset[<span>mt_rand</span>(0,<span>$_len</span><span>)];
</span><span>23</span> <span>  }
</span><span>24</span> <span> }
</span><span>25</span>  <span>//</span><span>生成背景</span>
<span>26</span>  <span>private</span> <span>function</span><span> createBg() {
</span><span>27</span>   <span>$this</span>->img = imagecreatetruecolor(<span>$this</span>->width, <span>$this</span>-><span>height);
</span><span>28</span>   <span>$color</span> = imagecolorallocate(<span>$this</span>->img, <span>mt_rand</span>(157,255), <span>mt_rand</span>(157,255), <span>mt_rand</span>(157,255<span>));
</span><span>29</span>   imagefilledrectangle(<span>$this</span>->img,0,<span>$this</span>->height,<span>$this</span>->width,0,<span>$color</span><span>);
</span><span>30</span> <span> }
</span><span>31</span>  <span>//</span><span>生成文字</span>
<span>32</span>  <span>private</span> <span>function</span><span> createFont() {
</span><span>33</span>   <span>$_x</span> = <span>$this</span>->width / <span>$this</span>-><span>codelen;
</span><span>34</span>   <span>for</span> (<span>$i</span>=0;<span>$i</span><<span>$this</span>->codelen;<span>$i</span>++<span>) {
</span><span>35</span>    <span>$this</span>->fontcolor = imagecolorallocate(<span>$this</span>->img,<span>mt_rand</span>(0,156),<span>mt_rand</span>(0,156),<span>mt_rand</span>(0,156<span>));
</span><span>36</span>    imagettftext(<span>$this</span>->img,<span>$this</span>->fontsize,<span>mt_rand</span>(-30,30),<span>$_x</span>*<span>$i</span>+<span>mt_rand</span>(1,5),<span>$this</span>->height / 1.4,<span>$this</span>->fontcolor,<span>$this</span>->font,<span>$this</span>->code[<span>$i</span><span>]);
</span><span>37</span> <span>  }
</span><span>38</span> <span> }
</span><span>39</span>  <span>//</span><span>生成线条、雪花</span>
<span>40</span>  <span>private</span> <span>function</span><span> createLine() {
</span><span>41</span>   <span>//</span><span>线条</span>
<span>42</span>   <span>for</span> (<span>$i</span>=0;<span>$i</span><6;<span>$i</span>++<span>) {
</span><span>43</span>    <span>$color</span> = imagecolorallocate(<span>$this</span>->img,<span>mt_rand</span>(0,156),<span>mt_rand</span>(0,156),<span>mt_rand</span>(0,156<span>));
</span><span>44</span>    imageline(<span>$this</span>->img,<span>mt_rand</span>(0,<span>$this</span>->width),<span>mt_rand</span>(0,<span>$this</span>->height),<span>mt_rand</span>(0,<span>$this</span>->width),<span>mt_rand</span>(0,<span>$this</span>->height),<span>$color</span><span>);
</span><span>45</span> <span>  }
</span><span>46</span>   <span>//</span><span>雪花</span>
<span>47</span>   <span>for</span> (<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>) {
</span><span>48</span>    <span>$color</span> = imagecolorallocate(<span>$this</span>->img,<span>mt_rand</span>(200,255),<span>mt_rand</span>(200,255),<span>mt_rand</span>(200,255<span>));
</span><span>49</span>    imagestring(<span>$this</span>->img,<span>mt_rand</span>(1,5),<span>mt_rand</span>(0,<span>$this</span>->width),<span>mt_rand</span>(0,<span>$this</span>->height),'*',<span>$color</span><span>);
</span><span>50</span> <span>  }
</span><span>51</span> <span> }
</span><span>52</span>  <span>//</span><span>输出</span>
<span>53</span>  <span>private</span> <span>function</span><span> outPut() {
</span><span>54</span>  <span>header</span>('Content-type:image/png'<span>);
</span><span>55</span>   imagepng(<span>$this</span>-><span>img);
</span><span>56</span>   imagedestroy(<span>$this</span>-><span>img);
</span><span>57</span> <span> }
</span><span>58</span>  <span>//</span><span>对外生成</span>
<span>59</span>  <span>public</span> <span>function</span><span> doimg() {
</span><span>60</span>   <span>$this</span>-><span>createBg();
</span><span>61</span>   <span>$this</span>-><span>createCode();
</span><span>62</span>   <span>$this</span>-><span>createLine();
</span><span>63</span>   <span>$this</span>-><span>createFont();
</span><span>64</span>   <span>$this</span>-><span>outPut();
</span><span>65</span> <span> }
</span><span>66</span>  <span>//</span><span>获取验证码</span>
<span>67</span>  <span>public</span> <span>function</span><span> getCode() {
</span><span>68</span>   <span>return</span> <span>strtolower</span>(<span>$this</span>-><span>code);
</span><span>69</span> <span> }
</span><span>70</span> }
Copy after login

注意:第16行中,要修改字体的路径,否则字体图片无法显示

实现,保存为captcha.php

<span>1</span> <span>session_start</span><span>();
</span><span>2</span> <span>require</span> './ValidateCode.class.php';  <span>//</span><span>先把类包含进来,实际路径根据实际情况进行修改。</span>
<span>3</span> <span>$_vc</span> = <span>new</span> ValidateCode();  <span>//</span><span>实例化一个对象</span>
<span>4</span> <span>$_vc</span>-><span>doimg();  
</span><span>5</span> <span>$_SESSION</span>['authnum_session'] = <span>$_vc</span>->getCode();<span>//</span><span>验证码保存到SESSION中</span>
Copy after login

页面使用

<span><</span><span>img  </span><span>title</span><span>="点击刷新"</span><span> src</span><span>="./captcha.php"</span><span> align</span><span>="absbottom"</span><span> onclick</span><span>="this.src='captcha.php?'+Math.random();"</span><span>></</span><span>img</span><span>></span>
Copy after login

转载自:一个漂亮的php验证码类(分享)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)