PHP 產生英文單詞驗證碼程式碼_PHP教程

來源:互聯網
上載者:User
如果你要產生單詞驗證碼我們必須一個己經做好的單詞庫了,下面我給大家準備了一個txt檔案,這裡面放了大量的單詞,我們只要產生時隨便讀到產生就KO了。

具體

代碼如下 複製代碼

$width=145;
$height = 45;

$authcode = vcaptcha_read_code('words.txt') ;

$bg = 'bg/captcha_bg3.jpg';

$img_type = 'png';

/* 驗證碼長度 */
$letters = strlen($authcode);

$img_bg = (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0)) ?
imagecreatefromjpeg($bg) : imagecreatefromgif($bg);
$bg_width = imagesx($img_bg);
$bg_height = imagesy($img_bg);

$img_org = ((function_exists('imagecreatetruecolor')) && PHP_VERSION >= '4.3') ?
imagecreatetruecolor($width, $height) : imagecreate($width, $height);

/* 將背景圖象複製原始圖象並調整大小 */
if (function_exists('imagecopyresampled') && PHP_VERSION >= '4.3') // GD 2.x
{
imagecopyresampled($img_org, $img_bg, 0, 0, 0, 0, $width, $height, $bg_width, $bg_height);
}
else // GD 1.x
{
imagecopyresized($img_org, $img_bg, 0, 0, 0, 0, $width, $height, $bg_width, $bg_height);
}
imagedestroy($img_bg);

$clr = imagecolorallocate($img_org, 255, 255, 255);

/* 繪製邊框 */
imagerectangle($img_org, 0, 0, $width - 1, $height - 1, $clr);

/* 獲得驗證碼的高度和寬度 */
$x = ($width - (imagefontwidth(5) * $letters)) / 2;
$y = ($height - imagefontheight(5)) / 2;
imagestring($img_org, 5, $x, $y, $authcode, $clr);

header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');

// HTTP/1.1
header('Cache-Control: private, no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);

// HTTP/1.0
header('Pragma: no-cache');
if ($img_type == 'jpeg' && function_exists('imagecreatefromjpeg'))
{
header('Content-type: image/jpeg');
imageinterlace($img_org, 1);
imagejpeg($img_org, false, 95);
}
else
{
header('Content-type: image/png');
imagepng($img_org);
}

imagedestroy($img_org);

function vcaptcha_read_code($wordlist_file)
{
$fp = @fopen($wordlist_file, 'rb');
if (!$fp) return false;

$fsize = filesize($wordlist_file);
if ($fsize < 32) return false; // too small of a list to be effective

if ($fsize < 128) {
$max = $fsize; // still pretty small but changes the range of seeking
} else {
$max = 128;
}

fseek($fp, rand(0, $fsize - $max), SEEK_SET);
$data = fread($fp, 128); // read a random 128 bytes from file
fclose($fp);
$data = preg_replace("/r?n/", "n", $data);

$start = strpos($data, "n", rand(0, 100)) + 1; // random start position
$end = strpos($data, "n", $start); // find end of word

return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes
}

使用方法:

代碼如下 複製代碼

將這段代碼複製到HTML裡,就可以了。還有這個的getCode函數是為了讓使用者看不清驗證碼,點擊可以切換,JavaScript指令碼如下:

http://file.bKjia.c0m/upload/mbdown/pic/2013/06/26/13659538282901.txt 這是單詞庫大家可下載。

http://www.bkjia.com/PHPjc/629617.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/629617.htmlTechArticle如果你要產生單詞驗證碼我們必須一個己經做好的單詞庫了,下面我給大家準備了一個txt檔案,這裡面放了大量的單詞,我們只要產生時隨...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.