首頁 > 後端開發 > php教程 > php-產生驗證碼 php驗證碼下載 php信箱驗證碼 php網站驗證

php-產生驗證碼 php驗證碼下載 php信箱驗證碼 php網站驗證

WBOY
發布: 2016-07-29 08:50:45
原創
1050 人瀏覽過
<span><?php
</span><span><em>/**
</em></span><span><em> * Created by PhpStorm.
</em></span><span><em> * User: 
</em></span><span><em> * Date: 2016/4/20
</em></span><span><em> * Time: 20:07
</em></span><span><em> */
</em></span><span><em></em></span><span>session_start()</span><span>;
</span><span>//</span><span>画一张背景图
</span><span>$image</span><span>=imagecreatetruecolor(</span><span>100</span><span>,</span><span>30</span><span>)</span><span>;
</span><span>$bgcolor</span><span>=imagecolorallocate(</span><span>$image</span><span>,</span><span>255</span><span>,</span><span>255</span><span>,</span><span>255</span><span>)</span><span>;
</span><span>imagefill(</span><span>$image</span><span>,</span><span>0</span><span>,</span><span>0</span><span>,</span><span>$bgcolor</span><span>)</span><span>;
</span><span>/*//</span><span>生成四位随机数字</span><span>   for($i=0;$i<4;$i&#43;&#43;){
</span><span>      $f
</span><span>      $f
</span><span>      $f
</span><span>      //</span><span>数字的显示位置</span><span>      $x=($i*100/4)&#43;rand(5,9);
</span><span>      $y=rand(5,10);
</span><span>      imagestring($image,$fonsize,$x,$y,$fontcontent,$foncolor);
</span><span>   }*/
</span><span>$captch_code</span><span>=</span><span>''</span><span>;
</span><span>//</span><span>生成数字字母混合验证码
</span><span>for</span><span>(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span>$i</span><span><</span><span>4</span><span>;</span><span>$i</span><span>&#43;&#43;){
</span><span>$fonsize</span><span>=</span><span>6</span><span>;
</span><span>$fontcolor</span><span>=imagecolorallocate(</span><span>$image</span><span>,</span><span>rand(</span><span>0</span><span>,</span><span>120</span><span>)</span><span>,</span><span>rand(</span><span>0</span><span>,</span><span>120</span><span>)</span><span>,</span><span>rand(</span><span>0</span><span>,</span><span>120</span><span>))</span><span>;
</span><span>$data</span><span>=</span><span>"abcdefghjklmnpqrstuvwxy13456789"</span><span>;
</span><span>$fontcontent</span><span>=substr(</span><span>$data</span><span>,</span><span>rand(</span><span>0</span><span>,</span><span>strlen(</span><span>$data</span><span>))</span><span>,</span><span>1</span><span>)</span><span>;
</span><span>$captch_code</span><span>.=</span><span>$fontcontent</span><span>;
</span><span>//</span><span>验证码的位置
</span><span>$x</span><span>=(</span><span>$i</span><span>*</span><span>100</span><span>/</span><span>4</span><span>)&#43;rand(</span><span>5</span><span>,</span><span>10</span><span>)</span><span>;
</span><span>$y</span><span>=rand(</span><span>5</span><span>,</span><span>10</span><span>)</span><span>;
</span><span>//
</span><span>imagestring(</span><span>$image</span><span>,</span><span>$fonsize</span><span>,</span><span>$x</span><span>,</span><span>$y</span><span>,</span><span>$fontcontent</span><span>,</span><span>$fontcolor</span><span>)</span><span>;
</span><span>}
</span><span>$_SESSION</span><span>[</span><span>'authcode'</span><span>]=</span><span>$captch_code</span><span>;
</span><span>//</span><span>添加干扰元素</span><span>:</span><span>点
</span><span>for</span><span>(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span>$i</span><span><</span><span>200</span><span>;</span><span>$i</span><span>&#43;&#43;){
</span><span>$pointcolor</span><span>=imagecolorallocate(</span><span>$image</span><span>,</span><span>rand(</span><span>55</span><span>,</span><span>200</span><span>)</span><span>,</span><span>rand(</span><span>55</span><span>,</span><span>200</span><span>)</span><span>,</span><span>rand(</span><span>55</span><span>,</span><span>200</span><span>))</span><span>;
</span><span>imagesetpixel(</span><span>$image</span><span>,</span><span>rand(</span><span>1</span><span>,</span><span>99</span><span>)</span><span>,</span><span>rand(</span><span>1</span><span>,</span><span>99</span><span>)</span><span>,</span><span>$pointcolor</span><span>)</span><span>;
</span><span>}
</span><span>//</span><span>添加干扰元素:线
</span><span>for</span><span>(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span>$i</span><span><</span><span>3</span><span>;</span><span>$i</span><span>&#43;&#43;){
</span><span>$linecolor</span><span>=imagecolorallocate(</span><span>$image</span><span>,</span><span>rand(</span><span>80</span><span>,</span><span>220</span><span>)</span><span>,</span><span>rand(</span><span>80</span><span>,</span><span>220</span><span>)</span><span>,</span><span>rand(</span><span>80</span><span>,</span><span>220</span><span>))</span><span>;
</span><span>imageline(</span><span>$image</span><span>,</span><span>rand(</span><span>1</span><span>,</span><span>99</span><span>)</span><span>,</span><span>rand(</span><span>1</span><span>,</span><span>29</span><span>)</span><span>,</span><span>rand(</span><span>1</span><span>,</span><span>99</span><span>)</span><span>,</span><span>rand(</span><span>1</span><span>,</span><span>29</span><span>)</span><span>,</span><span>$linecolor</span><span>)</span><span>;
</span><span>}
</span><span>   header(</span><span>'content-type: image/png'</span><span>)</span><span>;
</span><span>imagepng(</span><span>$image</span><span>)</span><span>;
</span><span>imagedestroy(</span><span>$image</span><span>)</span><span>;
</span><span>?></span>
登入後複製

以上就介紹了php-產生驗證碼,包含了驗證碼,php方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板