Rumah > pembangunan bahagian belakang > tutorial php > PHP验证码,php验证码代码_PHP教程

PHP验证码,php验证码代码_PHP教程

WBOY
Lepaskan: 2016-07-13 10:00:30
asal
892 orang telah melayarinya

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> }
Salin selepas log masuk

注意:第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>
Salin selepas log masuk

页面使用

<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>
Salin selepas log masuk

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/973822.htmlTechArticlePHP验证码,php验证码代码 设计一个验证码类,在需要的时候可以随时调用 验证码类,保存为ValidateCode.class.php 1 ? php 2 // 验证码类 3 session...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan