Heim > php教程 > php手册 > PHP验证码,php验证码代码

PHP验证码,php验证码代码

WBOY
Freigeben: 2016-06-13 09:09:31
Original
1579 Leute haben es durchsucht

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> }
Nach dem Login kopieren

注意:第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>
Nach dem Login kopieren

页面使用

<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>
Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage