php 驗證碼詳細產生與使用方法_PHP教程

來源:互聯網
上載者:User
注意:以下代碼需要開啟php教程的gd庫,修改php.in檔案的配置,把已經注釋掉的行之前的分號取消即可:extension=php_gd2.dll。

$width = 165;
$height = 120;
$image = imagecreatetruecolor($width,$height);
$bg_color = imagecolorallocate($image,255,255,255);
$tm = imagecolorallocate($image,255,0,0);
imagefilledrectangle($image,0,0,$width,$height,$bg_color);
imagefill($image,0,0,$bg_color);
/*
//$text = random_text(5);
$text = "ffff";
$font = 8;
$struft=iconv("gbk","utf-8",$text);
$x = imagesx($image);
$y = imagesy($image);
$fg_color = imagecolorallocate($image,233,14,91);
imagestring($image,$font,$x,$y,$struft,$fg_color);
$_session['captcha'] = $text;
*/
header("content-type:image/png");
imagepng($image);
imagedestroy($image);

執行個體二

validate.php

採用了session識別,稍微改進了一下目前網路上流傳的php驗證碼,加入雜點,數字顏色隨機顯示,控制4位元字顯示;

session_start();
//產生驗證碼圖片
header("content-type: image/png");
$im = imagecreate(44,18);
$back = imagecolorallocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);
//產生4位元字
for($i=0;$i<4;$i++){
$font = imagecolorallocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}

for($i=0;$i<100;$i++) //加入幹擾象素
{
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
imagepng($im);
imagedestroy($im);

$_session['vcode'] = $vcodes;
?>

下面看完整執行個體

@header("content-type:text/html; charset=utf-8");

//開啟session

session_start();

?>

php驗證碼樣本

驗證碼:



php判斷使用者輸入的驗證碼是否與系統產生的一致

//開啟session

session_start();

//得到使用者輸入的驗證碼,並轉換成大寫

$imgid_req = $_request['imgid'];

$imgid_req = strtoupper($imgid_req);

//驗證該字串是否註冊了session變數

if (session_is_registered($imgid_req)) {

echo "通過驗證!";

} else {

echo "驗證錯誤!";

}

//關閉session,以清除所有註冊過的變數

session_destroy();

?>

http://www.bkjia.com/PHPjc/629716.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/629716.htmlTechArticle注意:以下代碼需要開啟php教程的gd庫,修改php.in檔案的配置,把已經注釋掉的行之前的分號取消即可:extension=php_gd2.dll。 $width = 165; $hei...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.