這篇文章主要介紹了PHP生成圖檔驗證碼執行個體,同時介紹了點選切換(看不清?換一張)效果實作方法,需要的朋友可以參考下
這裡來看下效果:

現在讓我們來看下 PHP 代碼<?php
session_start();
function random($len) {
$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
mt_srand();
$strs = "";
for ($i = 0; $i < $len; $i++) {
$strs .= $srcstr[mt_rand(0, 30)];
}
return $strs;
}
//随機生成的字元串
$str = random(4);
//驗證碼圖檔的寬度
$width = 50;
//驗證碼圖檔的高度
$height = 25;
//聲明需要建立的圖層的圖檔格式
@ header("Content-Type:image/png");
//建立一個圖層
$im = imagecreate($width, $height);
//背景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
//模糊點顔色
$pix = imagecolorallocate($im, 187, 230, 247);
//字型色
$font = imagecolorallocate($im, 41, 163, 238);
//繪模糊作用的點
mt_srand();
for ($i = 0; $i < 1000; $i++) {
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}
//輸出字元
imagestring($im, 5, 7, 5, $str, $font);
//輸出矩形
imagerectangle($im, 0, 0, $width -1, $height -1, $font);
//輸出圖檔
imagepng($im);
imagedestroy($im);
$str = md5($str);
//選擇 cookie
//SetCookie("verification", $str, time() + 7200, "/");
//選擇 Session
$_SESSION["verification"] = $str;
?>
接下來隻要在頁面中調用就可以了:
如果想實作 "看不清?換一張" 效果,添加如下 JS 到頁面中function changing(){
document.getElementById('checkpic').src="/images/checkcode.php?"+Math.random();
}
以上就是本文的全部内容,希望對大家的學習有所幫助,更多相關内容請關注PHP中文網!
相關推薦: