溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP驗(yàn)證碼在驗(yàn)證頁面中的應(yīng)用

發(fā)布時(shí)間:2021-06-17 11:06:26 來源:億速云 閱讀:116 作者:chen 欄目:編程語言

這篇文章主要介紹“PHP驗(yàn)證碼在驗(yàn)證頁面中的應(yīng)用”,在日常操作中,相信很多人在PHP驗(yàn)證碼在驗(yàn)證頁面中的應(yīng)用問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”PHP驗(yàn)證碼在驗(yàn)證頁面中的應(yīng)用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

對(duì)于一個(gè)網(wǎng)站來說,肯定會(huì)需要一個(gè)提供驗(yàn)證功能的頁面。那么我們現(xiàn)在就需要考慮驗(yàn)證頁面中的驗(yàn)證碼的應(yīng)用了。我們接下來將要為大家具體講解有關(guān)PHP驗(yàn)證碼的相關(guān)應(yīng)用方法。

一、準(zhǔn)備一個(gè)展示并提交PHP驗(yàn)證碼的頁面

<?php            @header("content-type:text/html; charset=UTF-8");             //打開session             session_start();         ?>        <html>            <head>               <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />               <title>PHP驗(yàn)證碼示例</title>            </head>            <body>               驗(yàn)證碼:<br/>               <iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" ></iframe>               <br/>               <input type=button value="看不清,換一張" onclick="iimg.location.reload();">               <br>               <form action="validate.php" method="post">                   輸入驗(yàn)證碼:<input name="imgId" style="width:60">                   <input type="submit" value="確定">               </form>            </body>        </html>

二、以下是PHP驗(yàn)證碼生成頁面,該頁面在***頁面中被<img>調(diào)用

<?php        Header("Content-type: image/gif");         session_start();         //驗(yàn)證碼為隨機(jī)字符,以下是算法         $randval;         for($i=0;$i<7;$i++){             $randstr = mt_rand(ord('A'),ord('Z'));             srand((double)microtime()*1000000);             $randv = mt_rand(0,10);             if($randv%2==0){                $randval.=mt_rand(0,10);             }else{                $randval.=chr($randstr);             }         }         //注冊(cè)PHP驗(yàn)證碼到session         session_register($randval);         //以下是繪制驗(yàn)證碼圖         $height = 50;//圖高         $width = 100;//圖寬         $im = ImageCreateTrueColor($width, $height);         $white = ImageColorAllocate($im, 255, 255, 255);         $blue = ImageColorAllocate($im, 0, 0, 64);         ImageFill($im, 0, 0, $white);         srand((double)microtime()*1000000000);         ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue);         srand((double)microtime()*1000000);         ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue);         srand((double)microtime()*1000000);         ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);         ImageGIF($im);         ImageDestroy($im);         /*         需要注意的是:為了支持以上繪圖函數(shù),我們必須在PHP載入GD2圖形處理庫,可修改 php.ini 文件中的         ;extension=php_gd2.dll         為         extension=php_gd2.dll         即開啟GD2庫         */         ?>

三、這是驗(yàn)證頁面,提示PHP驗(yàn)證碼是否通過驗(yàn)證

<?php @header("content-type:text/html; charset=UTF-8");             //開啟session             session_start();             //得到用戶輸入的驗(yàn)證碼,并轉(zhuǎn)換成大寫             $imgId_req = $_REQUEST['imgId'];             $imgId_req = strtoupper($imgId_req);             //驗(yàn)證該字符串是否注冊(cè)了session變量             if (session_is_registered($imgId_req)) {                echo "<font color=blue >通過驗(yàn)證!</font>";             } else {                echo "<font color=red >驗(yàn)證錯(cuò)誤!</font>";             }             //關(guān)閉session,以清除所有注冊(cè)過的變量             session_destroy();         ?>

到此,關(guān)于“PHP驗(yàn)證碼在驗(yàn)證頁面中的應(yīng)用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI