驗證碼在表單實作越來越多了,但是用js的寫的驗證碼,總覺得不方便,所以學習了下php實作的驗證碼。
好吧,其實是沒有事情幹,但是又不想浪費時間,所以學習了下php實作驗證碼。正所謂,技多不壓身。而且,也可以封裝成函數,以後使用的時候也是很方便的,當然現在還沒有封裝。
現在來說說簡單的純數位驗證碼吧。
如果是初學者,建議按照我代碼的註釋 //數字 一步一步來。最簡單的方法,還是把整個程式碼複製走了。
新建一個captcha.php:
<?<span>php </span><span>//</span><span>10>设置session,必须处于脚本最顶部<span>session_start</span><span>(); </span><span>$image</span> = imagecreatetruecolor(100, 30); <span>//</span><span>1>设置验证码图片大小的函数 //5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);</span><span>$bgcolor</span> = imagecolorallocate(<span>$image</span>,255,255,255); <span>//</span><span>#ffffff //6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色</span> imagefill(<span>$image</span>, 0, 0, <span>$bgcolor</span><span>); </span><span>//</span><span>10>设置变量</span><span>$captcha_code</span> = ""<span>; </span><span>//</span><span>7>生成随机数字</span><span>for</span>(<span>$i</span>=0;<span>$i</span><4;<span>$i</span>++<span>){ </span><span>//</span><span>设置字体大小</span><span>$fontsize</span> = 6<span>; </span><span>//</span><span>设置字体颜色,随机颜色</span><span>$fontcolor</span> = imagecolorallocate(<span>$image</span>, <span>rand</span>(0,120),<span>rand</span>(0,120), <span>rand</span>(0,120)); <span>//</span><span>0-120深颜色 //设置数字</span><span>$fontcontent</span> = <span>rand</span>(0,9<span>); </span><span>//</span><span>10>.=连续定义变量<span>$captcha_code</span> .= <span>$fontcontent</span><span>; </span><span>//</span><span>设置坐标</span><span>$x</span> = (<span>$i</span>*100/4)+<span>rand</span>(5,10<span>); </span><span>$y</span> = <span>rand</span>(5,10<span>); imagestring(</span><span>$image</span>,<span>$fontsize</span>,<span>$x</span>,<span>$y</span>,<span>$fontcontent</span>,<span>$fontcolor</span><span>); } </span><span>//</span><span>10>存到session</span><span>$_SESSION</span>['authcode'] = <span>$captcha_code</span><span>; </span><span>//</span><span>8>增加干扰元素,设置雪花点</span><span>for</span>(<span>$i</span>=0;<span>$i</span><200;<span>$i</span>++<span>){ </span><span>//</span><span>设置点的颜色,50-200颜色比数字浅,不干扰阅读</span><span>$pointcolor</span> = imagecolorallocate(<span>$image</span>,<span>rand</span>(50,200), <span>rand</span>(50,200), <span>rand</span>(50,200<span>)); </span><span>//</span><span>imagesetpixel — 画一个单一像素</span> imagesetpixel(<span>$image</span>, <span>rand</span>(1,99), <span>rand</span>(1,29), <span>$pointcolor</span><span>); } </span><span>//</span><span>9>增加干扰元素,设置横线<span>for</span>(<span>$i</span>=0;<span>$i</span><4;<span>$i</span>++<span>){ </span><span>//</span><span>设置线的颜色</span><span>$linecolor</span> = imagecolorallocate(<span>$image</span>,<span>rand</span>(80,220), <span>rand</span>(80,220),<span>rand</span>(80,220<span>)); </span><span>//</span><span>设置线,两点一线</span> imageline(<span>$image</span>,<span>rand</span>(1,99), <span>rand</span>(1,29),<span>rand</span>(1,99), <span>rand</span>(1,29),<span>$linecolor</span><span>); } </span><span>//</span><span>2>设置头部,image/png<span>header</span>('Content-Type: image/png'<span>); </span><span>//</span><span>3>imagepng() 建立png图形函数</span> imagepng(<span>$image</span><span>); </span><span>//</span><span>4>imagedestroy() 结束图形函数 销毁$image</span> imagedestroy(<span>$image</span>);
〜接住頁提交的表單是到form. php的,所以還要有一個判斷的form.php程式碼:
<span><!</span><span>doctype html</span><span>></span><span><</span><span>html</span><span>></span><span><</span><span>head</span><span>></span><span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html; charset=UTF-8"</span><span>></span><span><</span><span>title</span><span>></span>确认验证码<span></</span><span>title</span><span>></span><span></</span><span>head</span><span>></span><span><</span><span>body</span><span>></span><span><</span><span>form </span><span>method</span><span>="post"</span><span> action</span><span>="./form.php"</span><span>></span><span><</span><span>p</span><span>></span>验证码: <span><</span><span>img </span><span>id</span><span>="captcha_img"</span><span> border</span><span>='1' </span><span>src</span><span>='./captcha.php?r=<?php </span><span>echo rand(); ?</span><span>></span><span>' /> </span><span><</span><span>a </span><span>href</span><span>="javascript:void(0)"</span><span> onclick</span><span>="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()"</span><span>></span>换一个?<span></</span><span>a</span><span>></span><span></</span><span>p</span><span>></span><span><</span><span>P</span><span>></span>请输入验证码:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>='authcode' </span><span>value</span><span>=''</span><span>/></</span><span>p</span><span>></span><span><</span><span>p</span><span>><</span><span>input </span><span>type</span><span>='submit' </span><span>value</span><span>='提交' </span><span>style</span><span>='padding:6px </span><span>5px;'</span><span>/></</span><span>p</span><span>></span><span></</span><span>body</span><span>></span><span></</span><span>html</span><span>></span>
顯示頁如下:
那麼,純數字的實現了,數字加英文的英文也應該不難了。要修改的程式碼 只是在captcha.php
將//7>產生隨機數字 修改成 //7>產生隨機的字母和數字,如果你真的很可愛的數字就修改這幾個字就認為可以實現的話,那麼恭喜你,你永遠保持快樂。腦殘兒童歡樂多。
廢話不多說了,拉代碼吧。
<?<span>php
</span><span>header</span>("Content-Type:text/html;charset=utf-8"); <span>//</span><span>设置头部信息
//isset()检测变量是否设置</span><span>if</span>(<span>isset</span>(<span>$_REQUEST</span>['authcode'<span>])){
</span><span>session_start</span><span>();
</span><span>//</span><span>strtolower()小写函数</span><span>if</span>(<span>strtolower</span>(<span>$_REQUEST</span>['authcode'])== <span>$_SESSION</span>['authcode'<span>]){
</span><span>//</span><span>跳转页面</span><span>echo</span> "<script language=\"javascript\">"<span>;
</span><span>echo</span> "document.location=\"./form.php\""<span>;
</span><span>echo</span> "</script>"<span>;
}</span><span>else</span><span>{
</span><span>//</span><span>提示以及跳转页面</span><span>echo</span> "<script language=\"javascript\">"<span>;
</span><span>echo</span> "alert('输入错误!');"<span>;
</span><span>echo</span> "document.location=\"./form.php\""<span>;
</span><span>echo</span> "</script>"<span>;
}
</span><span>exit</span><span>();
}</span>
一般而言,現在就已經夠用了。但就像動漫一樣,總會有番外。
那麼,我們來個漢字的番外吧。其實我也準備將漢字的驗證碼放到我的畢業設計裡面,雖然現在很流行滑動驗證碼,但是本人畢竟不是專門學習js的。 而且,還可以和答辯的老師說,我們驗證碼不需要素材,連圖片也是生成的,用自己的知識裝13,也沒有設麼的。<?<span>php </span><span>//</span><span>10>设置session,必须处于脚本最顶部<span>session_start</span><span>(); </span><span>$image</span> = imagecreatetruecolor(100, 30); <span>//</span><span>1>设置验证码图片大小的函数 //5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);</span><span>$bgcolor</span> = imagecolorallocate(<span>$image</span>,255,255,255); <span>//</span><span>#ffffff //6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色</span> imagefill(<span>$image</span>, 0, 0, <span>$bgcolor</span><span>); </span><span>//</span><span>10>设置变量</span><span>$captcha_code</span> = ""<span>; </span><span>//</span><span>7>生成随机的字母和数字</span><span>for</span>(<span>$i</span>=0;<span>$i</span><4;<span>$i</span>++<span>){ </span><span>//</span><span>设置字体大小</span><span>$fontsize</span> = 8<span>; </span><span>//</span><span>设置字体颜色,随机颜色</span><span>$fontcolor</span> = imagecolorallocate(<span>$image</span>, <span>rand</span>(0,120),<span>rand</span>(0,120), <span>rand</span>(0,120)); <span>//</span><span>0-120深颜色 //设置需要随机取的值,去掉容易出错的值如0和o</span><span>$data</span> ='abcdefghigkmnpqrstuvwxy3456789'<span>; </span><span>//</span><span>取出值,字符串截取方法 strlen获取字符串长度</span><span>$fontcontent</span> = <span>substr</span>(<span>$data</span>, <span>rand</span>(0,<span>strlen</span>(<span>$data</span>)),1<span>); </span><span>//</span><span>10>.=连续定义变量<span>$captcha_code</span> .= <span>$fontcontent</span><span>; </span><span>//</span><span>设置坐标</span><span>$x</span> = (<span>$i</span>*100/4)+<span>rand</span>(5,10<span>); </span><span>$y</span> = <span>rand</span>(5,10<span>); imagestring(</span><span>$image</span>,<span>$fontsize</span>,<span>$x</span>,<span>$y</span>,<span>$fontcontent</span>,<span>$fontcolor</span><span>); } </span><span>//</span><span>10>存到session</span><span>$_SESSION</span>['authcode'] = <span>$captcha_code</span><span>; </span><span>//</span><span>8>增加干扰元素,设置雪花点</span><span>for</span>(<span>$i</span>=0;<span>$i</span><200;<span>$i</span>++<span>){ </span><span>//</span><span>设置点的颜色,50-200颜色比数字浅,不干扰阅读</span><span>$pointcolor</span> = imagecolorallocate(<span>$image</span>,<span>rand</span>(50,200), <span>rand</span>(50,200), <span>rand</span>(50,200<span>)); </span><span>//</span><span>imagesetpixel — 画一个单一像素</span> imagesetpixel(<span>$image</span>, <span>rand</span>(1,99), <span>rand</span>(1,29), <span>$pointcolor</span><span>); } </span><span>//</span><span>9>增加干扰元素,设置横线<span>for</span>(<span>$i</span>=0;<span>$i</span><4;<span>$i</span>++<span>){ </span><span>//</span><span>设置线的颜色</span><span>$linecolor</span> = imagecolorallocate(<span>$image</span>,<span>rand</span>(80,220), <span>rand</span>(80,220),<span>rand</span>(80,220<span>)); </span><span>//</span><span>设置线,两点一线</span> imageline(<span>$image</span>,<span>rand</span>(1,99), <span>rand</span>(1,29),<span>rand</span>(1,99), <span>rand</span>(1,29),<span>$linecolor</span><span>); } </span><span>//</span><span>2>设置头部,image/png<span>header</span>('Content-Type: image/png'<span>); </span><span>//</span><span>3>imagepng() 建立png图形函数</span> imagepng(<span>$image</span><span>); </span><span>//</span><span>4>imagedestroy() 结束图形函数 销毁$image</span> imagedestroy(<span>$image</span>);
以上就介紹了二十四橋明月夜24,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。