Home > Backend Development > PHP Tutorial > How to implement verification code in php_PHP tutorial

How to implement verification code in php_PHP tutorial

WBOY
Release: 2016-07-12 09:07:47
Original
1170 people have browsed it

How to implement verification code in php

Verification codes are implemented more and more in forms, but verification codes written in js always feel inconvenient, so I learned verification codes implemented in php.


Verification codes are implemented more and more in forms, but verification codes written in js always feel inconvenient, so I learned about verification codes implemented in php. Well, I actually had nothing to do, but I didn’t want to waste time, so I learned how to implement verification codes in PHP. As the saying goes, having too many skills does not weigh you down. Moreover, it can also be encapsulated into a function, which is very convenient for future use. Of course, it is not encapsulated now.

Now let’s talk about the simple purely numerical verification code.

If you are a beginner, it is recommended to follow the comments of my code //Numbers step by step. The easiest way is to copy the entire code.

Create a new 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>
Copy after login

Then comes the code for the static page: 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">"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>
Copy after login

As you can see from index.html, the submitted form goes to form.php, so there is also a form.php code for judgment:

<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>
Copy after login

The page displayed is as follows:

Then, if pure numbers are implemented, it should not be difficult to add numbers and English. The code to be modified is just to change captcha.php to //7>Generate random numbers to //7> Generate random letters and numbers , if you are really cute and think it can be achieved by modifying these words, then congratulations, you will always be happy. Brain-disabled children have a lot of fun.

No more nonsense, let’s pull the code.  

<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>
Copy after login

The other two pages are not allowed to be modified.

Generally speaking, this is enough for now. But just like anime, there will always be extras.

So, let’s do a side story about Chinese characters. In fact, I am also planning to put the Chinese character verification code into my graduation project. Although sliding verification codes are very popular now, I am not specialized in learning js after all.

Moreover, you can also tell the defending teacher that our verification code does not require materials, even the pictures are generated, and there is nothing to pretend to be 13 with your own knowledge.

<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>
Copy after login

Other pages do not need to be modified.

The rendering is as follows:



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1059212.htmlTechArticleHow to implement verification code in php. Verification codes are implemented more and more in forms, but verification codes written in js , I always felt inconvenient, so I learned the verification code implemented in PHP. The verification code is in the form...
Related labels:
php
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