首页 >后端开发 >php教程 >php如何实现验证码_PHP教程

php如何实现验证码_PHP教程

WBOY
WBOY原创
2016-07-12 09:07:471181浏览

php如何实现验证码

验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。

\


验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。好吧,其实是没有事情干,但是又不想浪费时间,所 以学习了下php实现验证码。正所谓,技多不压身。而且,也可以封装成一个函数,以后使用的时候也是很方便的,当然现在未封装。

现在来说说简单的纯数字验证码吧。

如果是初学者,建议按照我代码的注释 //数字  一步步来。最简单的方法,还是把整个代码复制走了。

新建一个captcha.php:

<ol class="dp-c"><li class="alt"><span><span>php </span></span></li><li><span>    <span class="comment">//10>设置session,必须处于脚本最顶部</span><span> </span></span></li><li class="alt"><span>    session_start(); </span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$image</span><span> = imagecreatetruecolor(100, 30);        </span><span class="comment">//1>设置验证码图片大小的函数</span><span> </span></span></li><li><span>    <span class="comment">//5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);</span><span> </span></span></li><li class="alt"><span>    <span class="vars">$bgcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,255,255,255); </span><span class="comment">//#ffffff</span><span> </span></span></li><li><span>    <span class="comment">//6>区域填充 int imagefill(int im, int x, int y, int col)  (x,y) 所在的区域着色,col 表示欲涂上的颜色</span><span> </span></span></li><li class="alt"><span>    imagefill(<span class="vars">$image</span><span>, 0, 0, </span><span class="vars">$bgcolor</span><span>); </span></span></li><li><span>    <span class="comment">//10>设置变量</span><span> </span></span></li><li class="alt"><span>    <span class="vars">$captcha_code</span><span> = </span><span class="string">""</span><span>; </span></span></li><li><span>    <span class="comment">//7>生成随机数字</span><span> </span></span></li><li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><4;</span><span class="vars">$i</span><span>++){ </span></span></li><li><span>        <span class="comment">//设置字体大小</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$fontsize</span><span> = 6;         </span></span></li><li><span>        <span class="comment">//设置字体颜色,随机颜色</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$fontcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>, rand(0,120),rand(0,120), rand(0,120));            </span><span class="comment">//0-120深颜色</span><span> </span></span></li><li><span>        <span class="comment">//设置数字</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$fontcontent</span><span> = rand(0,9); </span></span></li><li><span>        <span class="comment">//10>.=连续定义变量</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$captcha_code</span><span> .= </span><span class="vars">$fontcontent</span><span>;     </span></span></li><li><span>        <span class="comment">//设置坐标</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$x</span><span> = (</span><span class="vars">$i</span><span>*100/4)+rand(5,10); </span></span></li><li><span>        <span class="vars">$y</span><span> = rand(5,10); </span></span></li><li class="alt"><span> </span></li><li><span>        imagestring(<span class="vars">$image</span><span>,</span><span class="vars">$fontsize</span><span>,</span><span class="vars">$x</span><span>,</span><span class="vars">$y</span><span>,</span><span class="vars">$fontcontent</span><span>,</span><span class="vars">$fontcolor</span><span>); </span></span></li><li class="alt"><span>    } </span></li><li><span>    <span class="comment">//10>存到session</span><span> </span></span></li><li class="alt"><span>    <span class="vars">$_SESSION</span><span>[</span><span class="string">'authcode'</span><span>] = </span><span class="vars">$captcha_code</span><span>; </span></span></li><li><span>    <span class="comment">//8>增加干扰元素,设置雪花点</span><span> </span></span></li><li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><200;</span><span class="vars">$i</span><span>++){ </span></span></li><li><span>        <span class="comment">//设置点的颜色,50-200颜色比数字浅,不干扰阅读</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$pointcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,rand(50,200), rand(50,200), rand(50,200));         </span></span></li><li><span>        <span class="comment">//imagesetpixel &mdash; 画一个单一像素</span><span> </span></span></li><li class="alt"><span>        imagesetpixel(<span class="vars">$image</span><span>, rand(1,99), rand(1,29), </span><span class="vars">$pointcolor</span><span>); </span></span></li><li><span>    } </span></li><li class="alt"><span>    <span class="comment">//9>增加干扰元素,设置横线</span><span> </span></span></li><li><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><4;</span><span class="vars">$i</span><span>++){ </span></span></li><li class="alt"><span>        <span class="comment">//设置线的颜色</span><span> </span></span></li><li><span>        <span class="vars">$linecolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,rand(80,220), rand(80,220),rand(80,220)); </span></span></li><li class="alt"><span>        <span class="comment">//设置线,两点一线</span><span> </span></span></li><li><span>        imageline(<span class="vars">$image</span><span>,rand(1,99), rand(1,29),rand(1,99), rand(1,29),</span><span class="vars">$linecolor</span><span>); </span></span></li><li class="alt"><span>    } </span></li><li><span> </span></li><li class="alt"><span>    <span class="comment">//2>设置头部,image/png</span><span> </span></span></li><li><span>    header(<span class="string">'Content-Type: image/png'</span><span>); </span></span></li><li class="alt"><span>    <span class="comment">//3>imagepng() 建立png图形函数</span><span> </span></span></li><li><span>    imagepng(<span class="vars">$image</span><span>); </span></span></li><li class="alt"><span>    <span class="comment">//4>imagedestroy() 结束图形函数  销毁$image</span><span> </span></span></li><li><span>    imagedestroy(<span class="vars">$image</span><span>); </span></span></li></ol>

接着就是静态页的代码了:index.html

<ol class="dp-c"><li class="alt"><span><span>doctype html> </span></span></li><li><span><html> </span></li><li class="alt"><span>    <head> </span></li><li><span>        <meta http-equiv=<span class="string">"Content-Type"</span><span> content=</span><span class="string">"text/html; charset=UTF-8"</span><span>> </span></span></li><li class="alt"><span>        <title>确认验证码title> </span></li><li><span>    head> </span></li><li class="alt"><span>    <body> </span></li><li><span>        <form method=<span class="string">"post"</span><span> action=</span><span class="string">"./form.php"</span><span>> </span></span></li><li class="alt"><span>            <p>验证码: <img  id=<span class="string" alt="php如何实现验证码_PHP教程" >"captcha_img"</span><span> border=</span><span class="string">'1'</span><span> src=</span><span class="string">'./captcha.php?r=echo rand(); ?>'</span><span> style=</span><span class="string">"width:100px; height:30px"</span><span> /> </span></span></li><li><span>                <a href=<span class="string">"javascript:void(0)"</span><span> onclick=</span><span class="string">"document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()"</span><span>>换一个?a> </span></span></li><li class="alt"><span>            p> </span></li><li><span>            <P>请输入验证码:<input type=<span class="string">"text"</span><span> name=</span><span class="string">'authcode'</span><span> value=</span><span class="string">''</span><span>/>p> </span></span></li><li class="alt"><span>            <p><input type=<span class="string">'submit'</span><span> value=</span><span class="string">'提交'</span><span> style=</span><span class="string">'padding:6px 5px;'</span><span>/>p>     </span></span></li><li><span>    body> </span></li><li class="alt"><span>html> <br /></span></li></ol>

从index.html可以看到,提交的表单是到form.php的,所以还要有一个判断的form.php代码:

<ol class="dp-c"><li class="alt"><span><span>php </span></span></li><li><span>    header(<span class="string">"Content-Type:text/html;charset=utf-8"</span><span>);            </span><span class="comment">//设置头部信息</span><span> </span></span></li><li class="alt"><span>    <span class="comment">//isset()检测变量是否设置</span><span> </span></span></li><li><span>    <span class="keyword">if</span><span>(isset(</span><span class="vars">$_REQUEST</span><span>[</span><span class="string">'authcode'</span><span>])){ </span></span></li><li class="alt"><span>        session_start(); </span></li><li><span>        <span class="comment">//strtolower()小写函数</span><span> </span></span></li><li class="alt"><span>        <span class="keyword">if</span><span>(</span><span class="func">strtolower</span><span>(</span><span class="vars">$_REQUEST</span><span>[</span><span class="string">'authcode'</span><span>])== </span><span class="vars">$_SESSION</span><span>[</span><span class="string">'authcode'</span><span>]){ </span></span></li><li><span>            <span class="comment">//跳转页面</span><span> </span></span></li><li class="alt"><span>            <span class="func">echo</span><span> </span><span class="string">"<script language=\"javascript\">"</span><span>; </span></span></li><li><span>            <span class="func">echo</span><span> </span><span class="string">"document.location=\"./form.php\""</span><span>; </span></span></li><li class="alt"><span>            <span class="func">echo</span><span> </span><span class="string">"</script>"</span><span>; </span></span></li><li><span>        }<span class="keyword">else</span><span>{ </span></span></li><li class="alt"><span>            <span class="comment">//提示以及跳转页面</span><span> </span></span></li><li><span>            <span class="func">echo</span><span> </span><span class="string">"<script language=\"javascript\">"</span><span>; </span></span></li><li class="alt"><span>            <span class="func">echo</span><span> </span><span class="string">"alert('输入错误!');"</span><span>; </span></span></li><li><span>            <span class="func">echo</span><span> </span><span class="string">"document.location=\"./form.php\""</span><span>; </span></span></li><li class="alt"><span>            <span class="func">echo</span><span> </span><span class="string">"</script>"</span><span>; </span></span></li><li><span>        } </span></li><li class="alt"><span>        <span class="func">exit</span><span>(); </span></span></li><li><span>    } <br /></span></li></ol>

显示页面如下:

\

那么,纯数字的实现了,数字加英文的也应该不难了。要修改的代码   只是在 captcha.php 将 //7>生成随机数字 修改成  //7>生成随机的字母和数字,如果你真的很可爱的就修改这几个字就认为可以实现的话,那么祝贺你,你永远保持快乐。脑残儿童欢乐多。

废话不多说了,拉代码吧。 

<ol class="dp-c"><li class="alt"><span><span>php </span></span></li><li><span>    <span class="comment">//10>设置session,必须处于脚本最顶部</span><span> </span></span></li><li class="alt"><span>    session_start(); </span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$image</span><span> = imagecreatetruecolor(100, 30);        </span><span class="comment">//1>设置验证码图片大小的函数</span><span> </span></span></li><li><span>    <span class="comment">//5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);</span><span> </span></span></li><li class="alt"><span>    <span class="vars">$bgcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,255,255,255); </span><span class="comment">//#ffffff</span><span> </span></span></li><li><span>    <span class="comment">//6>区域填充 int imagefill(int im, int x, int y, int col)  (x,y) 所在的区域着色,col 表示欲涂上的颜色</span><span> </span></span></li><li class="alt"><span>    imagefill(<span class="vars">$image</span><span>, 0, 0, </span><span class="vars">$bgcolor</span><span>); </span></span></li><li><span>    <span class="comment">//10>设置变量</span><span> </span></span></li><li class="alt"><span>    <span class="vars">$captcha_code</span><span> = </span><span class="string">""</span><span>; </span></span></li><li><span>    <span class="comment">//7>生成随机的字母和数字</span><span> </span></span></li><li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><4;</span><span class="vars">$i</span><span>++){ </span></span></li><li><span>        <span class="comment">//设置字体大小</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$fontsize</span><span> = 8;         </span></span></li><li><span>        <span class="comment">//设置字体颜色,随机颜色</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$fontcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>, rand(0,120),rand(0,120), rand(0,120));            </span><span class="comment">//0-120深颜色</span><span> </span></span></li><li><span>        <span class="comment">//设置需要随机取的值,去掉容易出错的值如0和o</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$data</span><span> =</span><span class="string">'abcdefghigkmnpqrstuvwxy3456789'</span><span>; </span></span></li><li><span>        <span class="comment">//取出值,字符串截取方法   strlen获取字符串长度</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$fontcontent</span><span> = </span><span class="func">substr</span><span>(</span><span class="vars">$data</span><span>, rand(0,</span><span class="func">strlen</span><span>(</span><span class="vars">$data</span><span>)),1); </span></span></li><li><span>        <span class="comment">//10>.=连续定义变量</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$captcha_code</span><span> .= </span><span class="vars">$fontcontent</span><span>;         </span></span></li><li><span>        <span class="comment">//设置坐标</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$x</span><span> = (</span><span class="vars">$i</span><span>*100/4)+rand(5,10); </span></span></li><li><span>        <span class="vars">$y</span><span> = rand(5,10); </span></span></li><li class="alt"><span> </span></li><li><span>        imagestring(<span class="vars">$image</span><span>,</span><span class="vars">$fontsize</span><span>,</span><span class="vars">$x</span><span>,</span><span class="vars">$y</span><span>,</span><span class="vars">$fontcontent</span><span>,</span><span class="vars">$fontcolor</span><span>); </span></span></li><li class="alt"><span>    } </span></li><li><span>    <span class="comment">//10>存到session</span><span> </span></span></li><li class="alt"><span>    <span class="vars">$_SESSION</span><span>[</span><span class="string">'authcode'</span><span>] = </span><span class="vars">$captcha_code</span><span>; </span></span></li><li><span>    <span class="comment">//8>增加干扰元素,设置雪花点</span><span> </span></span></li><li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><200;</span><span class="vars">$i</span><span>++){ </span></span></li><li><span>        <span class="comment">//设置点的颜色,50-200颜色比数字浅,不干扰阅读</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$pointcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,rand(50,200), rand(50,200), rand(50,200));         </span></span></li><li><span>        <span class="comment">//imagesetpixel &mdash; 画一个单一像素</span><span> </span></span></li><li class="alt"><span>        imagesetpixel(<span class="vars">$image</span><span>, rand(1,99), rand(1,29), </span><span class="vars">$pointcolor</span><span>); </span></span></li><li><span>    } </span></li><li class="alt"><span>    <span class="comment">//9>增加干扰元素,设置横线</span><span> </span></span></li><li><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><4;</span><span class="vars">$i</span><span>++){ </span></span></li><li class="alt"><span>        <span class="comment">//设置线的颜色</span><span> </span></span></li><li><span>        <span class="vars">$linecolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,rand(80,220), rand(80,220),rand(80,220)); </span></span></li><li class="alt"><span>        <span class="comment">//设置线,两点一线</span><span> </span></span></li><li><span>        imageline(<span class="vars">$image</span><span>,rand(1,99), rand(1,29),rand(1,99), rand(1,29),</span><span class="vars">$linecolor</span><span>); </span></span></li><li class="alt"><span>    } </span></li><li><span> </span></li><li class="alt"><span>    <span class="comment">//2>设置头部,image/png</span><span> </span></span></li><li><span>    header(<span class="string">'Content-Type: image/png'</span><span>); </span></span></li><li class="alt"><span>    <span class="comment">//3>imagepng() 建立png图形函数</span><span> </span></span></li><li><span>    imagepng(<span class="vars">$image</span><span>); </span></span></li><li class="alt"><span>    <span class="comment">//4>imagedestroy() 结束图形函数  销毁$image</span><span> </span></span></li><li><span>    imagedestroy(<span class="vars">$image</span><span>); </span></span></li></ol>

其他的两个页面,不许要修改。

\

一般而言,现在就已经够用了。但是就像动漫一样,总会有番外。

那么,我们来个汉字的番外吧。其实我也准备将汉字的验证码放到我的毕业设计里面,虽然现在很流行滑动验证码,但是本人毕竟不是专门学习js的。

而且,还可以和答辩的老师说,我们验证码不需要素材,连图片也是生成的,用自己的知识装13,也没有设么的。 

<ol class="dp-c"><li class="alt"><span><span>php </span></span></li><li><span>    <span class="comment">//11>设置session,必须处于脚本最顶部</span><span> </span></span></li><li class="alt"><span>    session_start(); </span></li><li><span> </span></li><li class="alt"><span>    <span class="comment">//1>设置验证码图片大小的函数</span><span> </span></span></li><li><span>    <span class="vars">$image</span><span> = imagecreatetruecolor(200, 60);         </span></span></li><li class="alt"><span>    <span class="comment">//5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);</span><span> </span></span></li><li><span>    <span class="vars">$bgcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,255,255,255); </span><span class="comment">//#ffffff</span><span> </span></span></li><li class="alt"><span>    <span class="comment">//6>区域填充 int imagefill(int im, int x, int y, int col)  (x,y) 所在的区域着色,col 表示欲涂上的颜色</span><span> </span></span></li><li><span>    imagefill(<span class="vars">$image</span><span>, 0, 0, </span><span class="vars">$bgcolor</span><span>); </span></span></li><li class="alt"><span>    <span class="comment">//7>设置ttf字体</span><span> </span></span></li><li><span>    <span class="vars">$fontface</span><span> = </span><span class="string">'FZYTK.TTF'</span><span>; </span></span></li><li class="alt"><span>    <span class="comment">//7>设置字库,实现简单的数字储备</span><span> </span></span></li><li><span>    <span class="vars">$str</span><span>=</span><span class="string">'天地不仁以万物为刍狗圣人不仁以百姓为刍狗这句经常出现在控诉暴君暴政上地残暴不仁把万物都当成低贱的猪狗来看待而那些高高在上的所谓圣人们也没两样还不是把我们老百姓也当成猪狗不如的东西但实在正取的解读是地不情感用事对万物一视同仁圣人不情感用事对百姓一视同仁执子之手与子偕老当男女主人公含情脉脉看着对方说了句执子之手与子偕老女方泪眼朦胧含羞地回一句讨厌啦这样的情节我们是不是见过很多但是我们来看看这句的原句死生契阔与子成说执子之手与子偕老于嗟阔兮不我活兮于嗟洵兮不我信兮意思是说战士之间的约定说要一起死现在和我约定的人都走了我怎么活啊赤裸裸的兄弟江湖战友友谊啊形容好基友的基情比男女之间的爱情要合适很多吧'</span><span>; </span></span></li><li class="alt"><span>    <span class="comment">//str_split()切割字符串为一个数组,一个中文在utf_8为3个字符</span><span> </span></span></li><li><span>    <span class="vars">$strdb</span><span> = </span><span class="func">str_split</span><span>(</span><span class="vars">$str</span><span>,3);     </span></span></li><li class="alt"><span>    <span class="comment">//>11</span><span> </span></span></li><li><span>    <span class="vars">$captcha_code</span><span> = </span><span class="string">''</span><span>; </span></span></li><li class="alt"><span>    <span class="comment">//8>生成随机的汉子</span><span> </span></span></li><li><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><4;</span><span class="vars">$i</span><span>++){ </span></span></li><li class="alt"><span>        <span class="comment">//设置字体颜色,随机颜色</span><span> </span></span></li><li><span>        <span class="vars">$fontcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>, rand(0,120),rand(0,120), rand(0,120));            </span><span class="comment">//0-120深颜色</span><span> </span></span></li><li class="alt"><span>        <span class="comment">//随机选取中文</span><span> </span></span></li><li><span>        <span class="vars">$in</span><span> = rand(0,</span><span class="func">count</span><span>(</span><span class="vars">$strdb</span><span>)); </span></span></li><li class="alt"><span>        <span class="vars">$cn</span><span> = </span><span class="vars">$strdb</span><span>[</span><span class="vars">$in</span><span>]; </span></span></li><li><span>        <span class="comment">//将中文记录到将保存到session的字符串中</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$captcha_code</span><span> .= </span><span class="vars">$cn</span><span>; </span></span></li><li><span>        <span class="comment">/*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,</span> </span></li><li class="alt"><span><span class="comment">        string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐标,颜色,字体路径,文本字符串</span> </span></li><li><span><span class="comment">        mt_rand()生成更好的随机数,比rand()快四倍*/</span><span> </span></span></li><li class="alt"><span>        imagettftext(<span class="vars">$image</span><span>, mt_rand(20,24),mt_rand(-60,60),(40*</span><span class="vars">$i</span><span>+20),mt_rand(30,35),</span><span class="vars">$fontcolor</span><span>,</span><span class="vars">$fontface</span><span>,</span><span class="vars">$cn</span><span>); </span></span></li><li><span>    } </span></li><li class="alt"><span>    <span class="comment">//11>存到session</span><span> </span></span></li><li><span>    <span class="vars">$_SESSION</span><span>[</span><span class="string">'authcode'</span><span>] = </span><span class="vars">$captcha_code</span><span>; </span></span></li><li class="alt"><span>    <span class="comment">//9>增加干扰元素,设置点</span><span> </span></span></li><li><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><200;</span><span class="vars">$i</span><span>++){ </span></span></li><li class="alt"><span>        <span class="comment">//设置点的颜色,50-200颜色比数字浅,不干扰阅读</span><span> </span></span></li><li><span>        <span class="vars">$pointcolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,rand(50,200), rand(50,200), rand(50,200));         </span></span></li><li class="alt"><span>        <span class="comment">//imagesetpixel &mdash; 画一个单一像素</span><span> </span></span></li><li><span>        imagesetpixel(<span class="vars">$image</span><span>, rand(1,199), rand(1,59), </span><span class="vars">$pointcolor</span><span>); </span></span></li><li class="alt"><span>    } </span></li><li><span>    <span class="comment">//10>增加干扰元素,设置线</span><span> </span></span></li><li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><4;</span><span class="vars">$i</span><span>++){ </span></span></li><li><span>        <span class="comment">//设置线的颜色</span><span> </span></span></li><li class="alt"><span>        <span class="vars">$linecolor</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>,rand(80,220), rand(80,220),rand(80,220)); </span></span></li><li><span>        <span class="comment">//设置线,两点一线</span><span> </span></span></li><li class="alt"><span>        imageline(<span class="vars">$image</span><span>,rand(1,199), rand(1,59),rand(1,199), rand(1,59),</span><span class="vars">$linecolor</span><span>); </span></span></li><li><span>    } </span></li><li class="alt"><span> </span></li><li><span>    <span class="comment">//2>设置头部,image/png</span><span> </span></span></li><li class="alt"><span>    header(<span class="string">'Content-Type: image/png'</span><span>); </span></span></li><li><span>    <span class="comment">//3>imagepng() 建立png图形函数</span><span> </span></span></li><li class="alt"><span>    imagepng(<span class="vars">$image</span><span>); </span></span></li><li><span>    <span class="comment">//4>imagedestroy() 结束图形函数  销毁$image</span><span> </span></span></li><li class="alt"><span>    imagedestroy(<span class="vars">$image</span><span>); </span></span></li></ol>

其他的页面也是不需要修改的。

效果图如下:

\

 



www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1059212.htmlTechArticlephp如何实现验证码 验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。 验证码在表单实...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn