Home > Backend Development > PHP Tutorial > PHP verification code, php verification code code_PHP tutorial

PHP verification code, php verification code code_PHP tutorial

WBOY
Release: 2016-07-13 10:00:30
Original
917 people have browsed it

PHP verification code, php verification code code

Design a verification code class that can be called at any time when needed

Verification code class, saved as 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

Note: In line 16, you need to modify the path of the font, otherwise the font image cannot be displayed

Implement and save as 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

Page usage

<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

Reprinted from: A beautiful php verification code class (share)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973822.htmlTechArticlePHP verification code, php verification code code design a verification code class, which can be called at any time when needed , saved as ValidateCode.class.php 1 ? php 2 // Verification code class 3 session...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template