ホームページ  >  記事  >  バックエンド開発  >  検証ページでの PHP 検証コードのアプリケーション分析_PHP チュートリアル

検証ページでの PHP 検証コードのアプリケーション分析_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-15 13:29:54995ブラウズ

ウェブサイトの場合、検証機能を提供するページが必ず必要になります。次に、検証ページでの検証コードの適用を検討する必要があります。次に詳しく説明します 1. PHP認証コードを表示・送信するページを用意します

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN>    </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    @header("content-type:text/html; </SPAN><SPAN class=attribute><FONT color=#ff0000>charset</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>UTF</FONT></SPAN><SPAN>-8");     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //打开session     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    session_start();     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>    </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>meta</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>http-equiv</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"Content-Type"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>content</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"text/html; charset=utf-8"</FONT></SPAN><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>PHP验证码示例</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>    </span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>    </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       验证码:</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>iframe</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"iimg"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>height</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"100"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>width</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>300</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>src</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"img.php"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>frameborder</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"0"</FONT></SPAN><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>iframe</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>button</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"看不清,换一张"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>onclick</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"iimg.location.reload();"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>form</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>action</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"validate.php"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"post"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>           输入验证码:</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"imgId"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>style</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"width:60"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>           </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"submit"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"确定"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>    </span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span>   </span>
</li>
</ol>

2. 以下はPHP認証コードです生成ページ、このページは最初のページで によって呼び出されます

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN>    </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>Header("Content-type: image/gif");     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>session_start();     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>//验证码为随机字符,以下是算法     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$randval;     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>for($</SPAN><SPAN class=attribute><FONT color=#ff0000>i</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>;$i</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>7</SPAN></FONT></STRONG><SPAN>;$i++){     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>randstr</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(ord('A'),ord('Z'));     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    srand((double)microtime()*1000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>randv</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(0,10);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    if($randv%</SPAN><SPAN class=attribute><FONT color=#ff0000>2</FONT></SPAN><SPAN>==0){     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>       $</SPAN><SPAN class=attribute><FONT color=#ff0000>randval.</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(0,10);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    }else{     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>       $</SPAN><SPAN class=attribute><FONT color=#ff0000>randval.</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>chr</FONT></SPAN><SPAN>($randstr);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    }     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>}     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>//注册PHP验证码到session     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>session_register($randval);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>//以下是绘制验证码图     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>height</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>50</FONT></SPAN><SPAN>;//图高     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>width</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>100</FONT></SPAN><SPAN>;//图宽     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>im</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>ImageCreateTrueColor</FONT></SPAN><SPAN>($width, $height);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>white</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>ImageColorAllocate</FONT></SPAN><SPAN>($im, 255, 255, 255);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>blue</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>ImageColorAllocate</FONT></SPAN><SPAN>($im, 0, 0, 64);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageFill($im, 0, 0, $white);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>srand((double)microtime()*1000000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>srand((double)microtime()*1000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>srand((double)microtime()*1000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageGIF($im);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageDestroy($im);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>/*     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>;</SPAN><SPAN class=attribute><FONT color=#ff0000>extension</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>php_gd2</FONT></SPAN><SPAN>.dll     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>为     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN></SPAN><SPAN class=attribute><FONT color=#ff0000>extension</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>php_gd2</FONT></SPAN><SPAN>.dll     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>即开启GD2库     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>*/     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span>    </span>
</li></ol>

3. これは検証ページであり、PHP 検証コードが認証に合格しました

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> @header("content-type:text/html; </SPAN><SPAN class=attribute><FONT color=#ff0000>charset</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>UTF</FONT></SPAN><SPAN>-8");     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //开启session     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    session_start();     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //得到用户输入的验证码,并转换成大写     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>imgId_req</FONT></SPAN><SPAN> = $_REQUEST['imgId'];     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>imgId_req</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>strtoupper</FONT></SPAN><SPAN>($imgId_req);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //验证该字符串是否注册了session变量     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    if (session_is_registered($imgId_req)) {     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>       echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>font</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>color</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>blue</FONT></SPAN><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>通过验证!</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>font</SPAN><SPAN class=tag>></span></font></strong><span>";     </span>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    } else {     </span></li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       echo "</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>font</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>color</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>red</FONT></SPAN><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>验证错误!</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>font</SPAN><SPAN class=tag>></span></font></strong><span>";     </span>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    }     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    //关闭session,以清除所有注册过的变量     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    session_destroy();     </span></li>
<li class=""><span>    </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>    </span>
</li>
</ol>

http://www.bkjia.com/PHPjc/446325.htmlwww.bkjia.com本当http://www.bkjia.com/PHPjc/446325.html技術記事 Web サイトの場合、検証機能を提供するページは必ず必要です。次に、検証ページでの検証コードの適用を検討する必要があります。次にやることは...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。