新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
PHP驗證碼在驗證頁面中的應用分析
對于一個網(wǎng)站來說,肯定會需要一個提供驗證功能的頁面。那么我們現(xiàn)在就需要考慮驗證頁面中的驗證碼的應用了。我們接下來將要為大家具體講解有關PHP驗證碼的相關應用方法。

#t#一、準備一個展示并提交PHP驗證碼的頁面
- @header("content-type:text/html; charset=UTF-8");
- //打開session
- session_start();
- ?>
- http-equiv="Content-Type" content="text/html; charset=utf-8" />
PHP驗證碼示例 - 驗證碼:
- type=button value="看不清,換一張" onclick="iimg.location.reload();">
action="validate.php" method="post"> - 輸入驗證碼: name="imgId" style="width:60">
- type="submit" value="確定">
二、以下是PHP驗證碼生成頁面,該頁面在***頁面中被調(diào)用
- Header("Content-type: image/gif");
- session_start();
- //驗證碼為隨機字符,以下是算法
- $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);
- }
- }
- //注冊PHP驗證碼到session
- session_register($randval);
- //以下是繪制驗證碼圖
- $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庫
- */
- ?>
三、這是驗證頁面,提示PHP驗證碼是否通過驗證
- @header("content-type:text/html; charset=UTF-8");
- //開啟session
- session_start();
- //得到用戶輸入的驗證碼,并轉(zhuǎn)換成大寫
- $imgId_req = $_REQUEST['imgId'];
- $imgId_req = strtoupper($imgId_req);
- //驗證該字符串是否注冊了session變量
- if (session_is_registered($imgId_req)) {
- echo " color=blue >通過驗證!";
- } else {
- echo " color=red >驗證錯誤!";
- }
- //關閉session,以清除所有注冊過的變量
- session_destroy();
- ?>
名稱欄目:PHP驗證碼在驗證頁面中的應用分析
網(wǎng)頁路徑:http://www.5511xx.com/article/cdshiij.html


咨詢
建站咨詢
